How to make actor an disappear when colliding with another actor an reappear in a random place

Hi everyone, I'm trying to make a snake game.

I wanted to know, how to make the snake collide with another actor, like an apple. then I want the apple to disappear and reappear in a random place within the walls. Would I need a table or something?

Please can you explain in as much detail as possible, because I'm quite new.
Thank you.

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    In the apple actor:

    When actor collides with [snake actor]
    .....Change attribute game.score to game.score + 1
    .....Spawn actor [apple actor] [x=random(0+self.Size.Width/2,1024-self.Size.Width/2)] [y=random(0+self.Size.Height/2,768-self.Size.Height/2)] relative to scene
    .....Destroy actor

    That assumes your scene is set to iPad Landscape (1024x768). The reason for including self.Size.Width/2 and self.Size.Height/2 is that an actor's position is its center so if you spawn an actor that is 50x50 at x=0, half of it (25 pixels) will be off the screen. Relative to "actor" means that the x and y position are offset from the actor (so if you put in 68 instead of the long expression I typed above, then it would spawn 68 pixels to the right or above the existing actor). Relative to "scene" means that the position is exactly that on the scene (x=68 would spawn 68 pixels to the right of the left edge of the scene since the bottom-left corner of a scene is x=0, y=0).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    In the apple actor:

    When actor collides with [snake actor]
    .....Change attribute game.score to game.score + 1
    .....Spawn actor [apple actor] [x=random(0+self.Size.Width/2,1024-self.Size.Width/2)] [y=random(0+self.Size.Height/2,768-self.Size.Height/2)] relative to scene
    .....Destroy actor

    That assumes your scene is set to iPad Landscape (1024x768). The reason for including self.Size.Width/2 and self.Size.Height/2 is that an actor's position is its center so if you spawn an actor that is 50x50 at x=0, half of it (25 pixels) will be off the screen. Relative to "actor" means that the x and y position are offset from the actor (so if you put in 68 instead of the long expression I typed above, then it would spawn 68 pixels to the right or above the existing actor). Relative to "scene" means that the position is exactly that on the scene (x=68 would spawn 68 pixels to the right of the left edge of the scene since the bottom-left corner of a scene is x=0, y=0).

    Hi tatiang, thanks for that.
    With the spawn actor bit, what do you put in the position bit?
    Also where are you supposed to put the x=random and y=random bits.
    Sorry for being a pain haha.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    See the attached demo.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53
    edited February 2015

    @tatiang said:
    See the attached demo.

    Hi tatiang, thank you, but there's one problem, when they both collide, it only works once. When they collide the second time, the Apple won't disappear and reappear in a random place. Do you have ideas of what I should do?

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    See the attached demo.

    HI tatiang, I'm not sure if you saw my last message, because I edited it.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Can you upload a screenshot of your rules to a file-sharing site and then post the link here?

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • MeepedMeeped Member Posts: 31

    I would make the apple change images to a transparent image and a timer where it destroys after 5 seconds or so. Then you can use the spawn code how you want, such as the one provided by tatiang.

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    Can you upload a screenshot of your rules to a file-sharing site and then post the link here?

    Hi tatiang, all I have done in the apple actor is, one rule, which says when actor collides with snake.
    Destroy this actor and spawn actor, (the snake), with the x=random and y=random.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    If you mean spawn actor (apple actor) then it should work the way you set it up.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53
    edited February 2015

    @tatiang said:
    If you mean spawn actor (apple actor) then it should work the way you set it up.

    Yes sorry, I meant the apple actor. I have got it to work now, so thank you.

    I have one other problem, I have actual walls in the game, and buttons in the game.

    Do you know how I would do it so the apple spawns inside of the walls and also not under the buttons?

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    If you mean spawn actor (apple actor) then it should work the way you set it up.

    Sorry, I forget to mention this before.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    @TheTiger said:
    Do you know how I would do it so the apple spawns inside of the walls and also not under the buttons?

    see below

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:

    Hi tatiang, I'm kind of confused. Would I put that behaviour in the apple actor? And would I need to delete the other spawn actor behaviour?

    Also, when you said, "assuming the apple actor has a width of 40". Did you mean the wall actor?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    @TheTiger said:
    Hi tatiang, I'm kind of confused.

    That's my fault! :p I was getting mixed up with another person who wanted to spawn at the edges of the scene.

    Would I put that behaviour in the apple actor?

    Yes.

    And would I need to delete the other spawn actor behaviour?

    Forget about the four spawns... you just need one. Replace the x and y positions in the Spawn Behavior with:

    x=random(0+40/2,1024-40/2)
    y=random(0+40/2,768-40/2)

    Also, when you said, "assuming the apple actor has a width of 40". Did you mean the wall actor?

    No, I meant the apple actor. Try it without the 40/2 and you'll see that sometimes part of the apple actor is off of the screen. This is due to the fact that GameSalad considers the center of the actor as its position. If you spawn or place an actor at x=0, then half of it is beyond the left side of the scene boundary. If you spawn or place it at x=0+self.Size.Width/2, then it is right up against the edge of the scene.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:

    Hi tatiang ive tried it, and it doesn't seem to be working.
    My apple has actor has a width of 31.
    I have put in the spawn actor... x=random(0+31/2,320-31/2)
    and y=random(0+31/2,480-31/2)

    I have decided to only have on wall and that's at the bottom, so im not sure if that's why its not working, and the height of that wall is 326.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    @TheTiger said:
    Hi tatiang ive tried it, and it doesn't seem to be working.

    Can you be more specific? Is it spawning but in the wrong locations? Is it not spawning at all? What happens when you put a Log Debugging Statement in the actor that is being spawned and use the expression editor to add self.Position.X and/or self.Position.Y and then watch the Debugger window?

    I have put in the spawn actor... x=random(0+31/2,320-31/2)

    and y=random(0+31/2,480-31/2)

    Make sure you have Relative to Scene set in the Spawn Behavior and not Relative to Actor. Those values should spawn an actor at an X position between 15.5 and 304.5 and a Y position between 15.5 and 448.5.

    On a side note, are you purposely building on a Legacy platform (e.g. iPhone 4)? I recommend using iPad Portrait or Landscape for any iPhone/iPad projects.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:

    When the snake actor collides with the apple actor, the Apple spawns in the bottom left corner of the screen, with most of it of the page.

    I have also tried it in a different way. But there's a slight problem
    In the spawn actor I have put...
    Random(0+self.size.width/2,320-self.size.width/2)
    And also... random(0+self.size.height/2,379-self.size.height/2)
    In that second bit, I subtracted the height of the wall actor which is 101 from 480, which is 379.

    However, it's done it from bottom to top, so it spawns in the wall, which is at the bottom, but it doesn't spawn in the top quarter of the scene.

    So what do you think I should do, so it spawns from 0-379, from top to bottom?

    Also thank you for the recommendation of using iPad.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    When the snake actor collides with the apple actor, the Apple spawns in the bottom left corner of the screen, with most of it of the page.

    It sounds like there's a problem with one or both of the expressions you entered. Are you selecting attribute names from the drop-down menu? If you just type them in, they won't work.

    As a test, what happens when you try to spawn an actor at x=random(0,320), y=random(0,380) relative to scene? Does that spawn apples at various positions in the scene?

    What would be helpful at this point is to see a screenshot of your scene and a screenshot of your spawn actor rule. You can upload them to a file-sharing service and then embed or post the link here.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:

    Hi tatiang, I just tried that, and it doesn't spawn at various positions in the screen, it just does the same thing. As in, spawns in the bottom left corner.

    It seems to be working when I do... random(0+self.size.height/2,379-self.size.height/2)
    But the only problem is that it does it from bottom to top, so it doesn't spawn in the top quarter but it spawns in the rest of the scene.
    So do you how I would do it, so it's top to bottom, not bottom to top?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    I'm not sure what you tried since the quote doesn't appear above.

    Positions in GameSalad are measured from left to right for X and from bottom to top for Y. So the bottom of the screen is Y=0. You need to add the wall height to the 0+... part of the expression instead of subtracting it from the second part.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    I'm not sure what you tried since the quote doesn't appear above.

    Positions in GameSalad are measured from left to right for X and from bottom to top for Y. So the bottom of the screen is Y=0. You need to add the wall height to the 0+... part of the expression instead of subtracting it from the second part.

    Could you give me an example on how to do that please?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    If your wall height is 101 and your scene height is 480 and your spawned actor height is 31, then you would do this for the Y position:

    random(0+101+31/2,480-31/2)

    Note that self.Size.Height in the spawner actor is going to be that actor's height, not the height of the spawned actor.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • osakamitsuosakamitsu Member Posts: 70

    @TheTiger The official game template that comes with GameSalad called Space Rocks does this very thing. It doesn't come with the game salad download anymore so you have to download it from this link:

    http://gamesalad.com/manuals

    There is like 14 more templates than what is in the download.

  • TheTigerTheTiger Member Posts: 53
    edited February 2015

    @tatiang said:
    If your wall height is 101 and your scene height is 480 and your spawned actor height is 31, then you would do this for the Y position:

    random(0+101+31/2,480-31/2)

    Note that self.Size.Height in the spawner actor is going to be that actor's height, not the height of the spawned actor.

    Thank you for that tatiang, it worked.

    I was wondering if you could help me with something else.

    I wanted to know, how I would end the game, when my actor(the snake) touches the edge of the screen.
    So when my actor(the snake) touches the edge of the screen, I want it to say game over, display the score, a retry button and also go back to main menu button.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2015

    You can use a Change Scene or Pause Game behavior to switch to another scene that contains actors that show the score and/or have rules to change to the menu scene. Trigger it with a collision rule.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53
    edited February 2015

    @tatiang said:
    You can use a Change Scene or Pause Game behavior to switch to another scene that contains actors that show the score and/or have rules to change to the menu scene. Trigger it with a collision rule.

    Hi tatiang, I want to have the game over, score, retry and back to main menu on the same screen as where you play the game, so I want the game in the backround, so would I have to create another scene? How would I do it?

    Thanks.

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    You can use a Change Scene or Pause Game behavior to switch to another scene that contains actors that show the score and/or have rules to change to the menu scene. Trigger it with a collision rule.

    Ive done the pause game behaviour, so that freezes the game, so what would I have to do, so add all of those things on the same screen?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Yes, you would create a new scene for the score display and buttons and then use the Pause Game behavior.

    Here's a tutorial I made:

    I'm sure there are better videos out there. ;)

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TheTigerTheTiger Member Posts: 53

    @tatiang said:
    Yes, you would create a new scene for the score display and buttons and then use the Pause Game behavior.

    Here's a tutorial I made:

    I'm sure there are better videos out there. ;)

    Ok, thanks for that tatiang.

  • TheTigerTheTiger Member Posts: 53

    Hi tatiang, I've been trying to figure something out, i think it may be quite simple. But I'm not sure. All I wanted to do was, when the game starts, I have text saying "tap arrows to start the game", which is on the game screen. So how would I make that text disappear when the arrow buttons are pressed?

Sign In or Register to comment.