How do I get one actor to follow another? (top down view)

sciTunes

  • Posts: 3
I have been using gamesalad for about two years. To do this in GS I would create two game attributes (heroX and heroY) and constrain them to the x and y of the hero. Then in the enemy I could use these two attributes so that the enemy would "know" where the hero is. How can I do that with stencyl?

Thanks!

Greg

  • *
  • Posts: 1260
I've been experimenting with multi-part Actors lately (in this game) and the following is the best approach I could come up with.

1) Add both Actors to your scene, in roughly the right position
2) Add a behavior similar to the following, to the Actor that you want to follow your main Actor.  Basically it "finds" the main player Actor and sets that to an attribute.  Then, every frame it gets the position of the player and updates its own location accordingly. 

Doing this by itself should work, however there's a known issue that it causes the follower Actor to "jitter" a bit.  So to work around this, I added a block to hide the normal rendering of the follower Actor, and in the "when drawing" wrapper I use some blocks to draw the image there. 

Alternatively you could have your first Actor just spawn the 2nd Actor in a behavior.  However, I wanted to be able to reuse this for multiple parts (head, gun, arm, etc...) so I went with this route.

Rob

  • *
  • Posts: 1268
Welcome, SciTunes! Glad you have a chance to try stencyl out.

@Greg, following your work through these years, I believe you have a lot of good generalized behaviors that people can use, I think it would be a great idea if we can put up more of your work on our featured behavior list on Forge.

It would really streamline a lot of newer folks coming to our site and pick up things faster.
GOLDEN RULE #1 : SAVE YOUR GAMES FREQUENTLY
IOS/XCODE DEBUGGING
http://www.stencyl.com/help/view/xcode-ios-troubleshoot/
WATCH STENCYLWORKS VIDEO TUTORIAL BY SUNRISEKINGDOM
http://www.youtube.com/watch?v=KDfRfjzr9j4&feature=channel_video_title

sciTunes

  • Posts: 3
Thanks guys.

I think I asked my question wrong. What I am trying to do is have a main actor controlled with the mouse (got that working fine) and baddies that chase the main actor.

I was thinking there should be away to constrain the main actors x y coordinates to a global attribute that can be "read" by the baddies. Then I would use trig to constrain the baddies' rotation to point towards the main actor. Can't seem to get that to work.

Is there away to get actor 1 to "see" the x,y coordinates of actor 2?

Thanks!

Hectate

  • *
  • Posts: 4643
For the enemies' "Chase Player" behavior, I'd recommend the following since you're only ever likely to have 1 player at a time.

Create an attribute of the type Actor. Name it "Player" or something similar.

Under "When Created", drop in the wrapper block "For Each Actor Of Type" and select your player's actor type in its selection box.

Inside that wrapper block, put the attribute setter block for "Set Player To" and put the "The Actor" block (from the same section of the palette as the wrapper block itself) as the actor to set the attribute to.

What'll happen is whenever an enemy using this behavior is created, it'll check the entire scene for all the actors of your player type and assign the one that it finds (or the LAST one that it finds, in case of multiple) to the "Player" attribute. Once you have done that, at any other point in time (such as during gameplay) you can get the Player attribute's location information via all the other Actor blocks such as the X and Y that you need by telling it to use the Player attribute instead of the default of "Self".

I'd give you an example but I'm afraid I don't have access to SW here at work right now. At one point I had created a simple example behavior for someone that did exactly this (Enemy Chases Player) that was on Forge, but it may have been lost in the transition to launch.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Greg

  • *
  • Posts: 1260
The way Hectate described is normally what I do.  There's also a behavior called "Seek Target" in the "Advanced Motion Pack" resource pack.  To find it, open StencylForge and either search for that name, or browse the Resource Pack section and it should be one of the featured packs.

The catch here is that this behavior relies on Actor Instance customization.  This means that you would first add this behavior to your enemy Actors.  Then, when you add your enemies to the scene in the Scene Designer, you double click on them and choose "Enable Customization".  From here you click in the "Target" attribute box and it will allow you to select your target Actor (the player).