0 Favourites

# Putting a condition onto an action?

• 2 posts
• I'm new to Construct 2 and I'm making a maze based game with enemy sprites. I want to have them randomly generate at the start of the layout. But by doing that they often generate in the middle of a wall and get stuck. I also want them to never generate within line of sight of the player. How to I tell it to randomly generate an object at any location on the layout, but never in a wall or in line of sight? Thank you.

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• edit: if you want a less scientific solution, you could for example write your action like this:

localvar = 0

while

localvar=0: spawn enemy at x = floor(random(gridsize+1))*tilesize y = floor(random(gridsize+1))*tilesize

-- (invert) enemy overlaps wallsprite AND distance enemy-player > x: set localvar=1

-- else: destroy enemy

Depends how you handle your maze generation pretty much.

Example:

<img src="http://666kb.com/i/civ25q6qg342xq1zr.png" border="0" />

and you've put your maze information in an array.

1, 2, 3... are X.

A, B, C... are Y.

Grey = walls, the value of the array at this (X,Y)-position will be 1.

White = ways, the value of the array at this (X,Y)-position will be 0.

Red = vision, the value of the array at this (X,Y)-position will be 2.

Then upon enemy creation you would only allow enemies to spawn on tiles where the corresponding array value is 0.

How to get there?

• The first two (way and walls) are straight forward - when you generate your maze you can input this data into the array.
• now the red squares:

* a for loop exploring into each direction starting from the player position P would do the trick. Set the values to two and stop the loop once it hits a wall (don't set that value to two).

* if you don't wanna spawn enemies near your player, set all array values that are 0 and within +-x of your player's X and Y to 2.

After that use a while loop with local variables, where you roll a random number for x and y, the number going from 0 to your gridsize in x or y. Then you check if the array is 0 for that x and y. If yes: spawn the enemy at x*tilesize,y*tilesize and stop the loop (by setting your while-condition to false). If no (= else), it will loop again.

Once you've successfully spawned your enemy, use array: for each element AND array.curvalue = 2: set array at (array.curx, array.cury) to 0.

• 2 posts