0 Favourites

Randomly generate sprite

  • 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.

  • 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

  • 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

  • What are the conditions for your while?

    Seems like they are never true and as such you've created an endless loop..

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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)

  • 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 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

  • 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. thanks to your "overlapping at offset" idea too. it helped, but still right after doing that, I had a new problem right away. xD

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)