Randomly generate sprite

Just started using Construct 2? Post your questions here

Post » Thu Dec 31, 2015 3:18 am

I'm trying to make a classic snake game. Upon being eaten by the snake, the apple is supposed to be regenerated at a random location. However, I don't want it to overlaps snake head / snake body / other sprites on being created. What condition should I include to make this happen?

For a "GenerateApple" function, I plan to first randomly create a new apple
Then, I will sub-event the 'Or' overlapping condition (for the various objects) under a 'While' condition to keep destroy -> regenerate an apple WHILE unintended overlapping happens
Will this work?

Thanks a lot.
B
6
S
1
Posts: 14
Reputation: 328

Post » Thu Dec 31, 2015 3:45 am

In your spawn condition randomize the apple placement and check for overlap before spawning (if not overlapping).
Have this placement repeat until successful. Unless you have a ton of stuff on screen it should work fine.
If you find the apple is too close to the snake/body/head then also condition distance is greater than distance from head.x head.y body.x body.y +50.. etc, etc
Check out my completed games on the Arcade! Capt. Doe Save the Universe! & Clicker Wizard!
B
60
S
15
G
8
Posts: 620
Reputation: 8,972

Post » Thu Dec 31, 2015 6:28 am

To repeatedly spawn/destroy apples until it is at desirable distance from other objects, I need to use While loop right?
However, when I tried including While, the game hangs upon collision between snake and apple
B
6
S
1
Posts: 14
Reputation: 328

Post » Thu Dec 31, 2015 12:05 pm

What are the conditions for your while?
Seems like they are never true and as such you've created an endless loop..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sat Jan 02, 2016 2:11 am

I simply put 'overlapping at offset with snakehead' OR 'overlapping at offset with snakebody' OR etc. (same condition with the other sprites which I don't want to be overlapped with)
B
6
S
1
Posts: 14
Reputation: 328

Post » Sun Jan 03, 2016 12:58 am

skymania wrote:I simply put 'overlapping at offset with snakehead' OR 'overlapping at offset with snakebody' OR etc. (same condition with the other sprites which I don't want to be overlapped with)


Why would you use a "while" for this?
What are the actions attached to the while?
Will those actions do something to stop the "is overlapping at offset"-condition?
If not you have an endless loop..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sun Jan 03, 2016 7:25 am

I do realize that the conditions will lead to the formation of an endless loop. How should I place the conditions to avoid overlapping of the apple "upon being created" only?
(this event is a sub-event of a function, for some reason I cannot employ the condition "apple on created")

[EDIT] I just realized if I don't use a while and put call on function "generateApple" as the action for the overlapping OR block conditions then it will create a loop until the apple is in a satisfying position
B
6
S
1
Posts: 14
Reputation: 328

Post » Sun Feb 28, 2016 5:23 pm

@skymania
can u upload the .capx for your example? or a screenshot maybe? i have the same problem as yours but a little different. tsk! Like your problem, i have to spawn a sprite anywhere on the screen, but the sprite that i needed to spawn is at least 20, without overlapping any object... i tried destroying the sprite if it overlaps any object but i noticed that it's not a good idea... I am now trying to just find another position for my sprite once it overlaps an object. but then a new problem arise, it's not "snapped to grid"... i suppose you can imagine that. So, again if you could upload the screenshot of your event sheet or the .capx, it would be of great help to me. :D thanks to your "overlapping at offset" idea too. it helped, but still right after doing that, I had a new problem right away. xD
B
5
Posts: 12
Reputation: 253


Return to Beginner's Questions

Who is online

Users browsing this forum: RegPhoenix and 0 guests