help with enemy movement on a platformer???

CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
ok so i need my enemies to keep moving left and right,
eg;
one of my enemies will start in the middle of a platform and start to move left, when it reaches the end of the platform, it goes right, and then left and so on, so the enemy wont fall off the platform.

any help would be great thanks!
«1

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster

    Constrain X Position to:

    AAA*sin(game.time*BBB)+CCC

    . . . . . . . . . . . . .


    AAA = range of the left to right movement, so if you want the enemy to 'patrol' 100 pixels to the left of his starting position and then 100 pixels to the right his starting position . . . you would make this value 100.

    BBB = how fast you want the enemy to move.

    CCC = the enemy's starting position.


    . . . . . . . . . . . . .


    So an actor moving from left to right on a horizontal iPad screen (1024 pixels wide) would be:

    512*sin(game.time*100)+512

    (the '100' here is an arbitrary speed)
  • vuifahvuifah Member Posts: 62
    @Socks

    Very useful. Will use it on my next game.
    Thank you very much.
  • SocksSocks London, UK.Member Posts: 12,822
    @Socks

    Very useful. Will use it on my next game.
    Thank you very much.

    @vuifah

    :)>-
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    Thanks heaps @socks Gona try this out now ;)
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    @Socks i did that and it went all wierd........ i have all enemies on different platform scattered across the game. i put that rule into the prototype so all have the same rule, then clicked play.

    when in preview all of the enemies were above eachother and moving from left to right, any reason why this is happening? :(
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    . . . all of the enemies were above each other and moving from left to right, any reason why this is happening
    If you put the same rule in all the actors - and the rule says constrain X to a certain position - then all your actors will be constrained to that X position.

    Make use of [CCC = the enemy's starting position] to give each actor a unique X position.
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    oohh now i get it cheers @Socks but just one other thing, how can i make it so the actor animates left and right? so when the actor walks left, it animates left walk, and right walk when right? because i dont know how to do this with this formula...
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    oohh now i get it cheers @Socks but just one other thing, how can i make it so the actor animates left and right? so when the actor walks left, it animates left walk, and right walk when right? because i dont know how to do this with this formula...
    Change the animation to whatever you want when the actor reaches the end of his patrol area.

    Example attached:
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    . . . i have all enemies on different platform scattered across the game. . . . (
    P.S. . . .


    If you want multiple actors all following the same constrain behaviour, but with their own unique X position then stick this into the master enemy actor . . . .

    Make an attribute (let's call it 'MyUniqueXPosition') use this to store the actor's current X position, then in your constrain behaviour make the CCC part = MyUniqueXPosition.

    So:

    Change attribute MyUniqueXPosition to Self.Positon X
    AAA*sin(game.time*BBB)+MyUniqueXPosition.

    . . . . That means the actor will patrol left to right (repeatedly) with a range of AAA, at a speed of BBB and with its patrol range centred on whatever the actor's original X position was.




  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    oh that would be so much easier thanks heaps @Socks
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    oh that would be so much easier thanks heaps @Socks

    One final point, if you use the above method (the 'MyUniqueXPosition' attribute) then obviously the method for detecting that the actor has reached the edges of his patrol areas (so you can switch animations from left > right < left > right) mentioned above, which is a simple static X value won't work as the actors will all be at different locations on the screen - so instead use the MyUniqueXPosition attribute to check whether the actor has reached either end of its range.

    Something like this: (let's assume the patrol range is 100 pixels each way, left and right).

    If X position = (MyUniqueXPosition-100) then use the right facing animation.
    If X position = (MyUniqueXPosition+100) then use the left facing animation.


    Hope that makes sense.

  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    yep now that makes it even easier wow thanks heaps!!! :)
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    @Socks mmmmm inside the prototype of the enemies i changed the new created attribute to self position x, and i did constrain and everything right but all the actors are now all above eachother??? why :(
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    @Socks mmmmm inside the prototype of the enemies i changed the new created attribute to self position x, and i did constrain and everything right but all the actors are now all above eachother??? why :(
    You've done something wrong.
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    ok here is exactly what iv done:

    created a self integer attribute called constrainingXposition

    i then constrained self.position.x to 60*sin(game.time*15)+constrainingXposition

    next i changed attribute self.constrainingXposition to self.position.X

    rule for animation(which isnt working :(::
    rule if attribute self.position.x = (self.constrainingXposition-60)
    then animated right

    rule animation left;
    rule if attribute self.position.x = (self.constrainingXposition+60)
    then animated left

    please help me out im confused @Socks
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    ok here is exactly what iv done:

    1) created a self integer attribute called constrainingXposition
    2) i then constrained self.position.x to 60*sin(game.time*15)+constrainingXposition
    3) next i changed attribute self.constrainingXposition to self.position.X


    Here is what your process above does . . .

    1) You create a variable, it's called constrainingXposition with a default value of zero.

    2) You then tell an actor to constrain itself to 60 x the sine of an ever increasing angle + the value of the constrainingXposition variable (which is zero) . . so off it goes, doing it's thing.

    3) You then change the constrainingXposition variable to the actor's current position.


    . . . . if you can't see what's wrong here I'll tell you if you want me to, but it's always good to see these things for yourself ! Let me know if you want me to tell you what's wrong. :)>-
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    it would be good if you could tell me whats wrong @Socks iv been staring at it for a while :/

    but my guess is that its got something to do with the attribute??
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    it would be good if you could tell me whats wrong @Socks iv been staring at it for a while :/
    Lol ! I'm just being cruel ;) Surely you can see what's up here ? (answer below*)

    1) You create a variable with a default value of zero.

    2) You then tell an actor to constrain itself to move left and right about this variable.

    3) You then change the variable to the actor's current position.









    Swap 2 and 3.
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    so i put 2 before 3?? @Socks
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    haha it worked!! @Socks thanks i didnt realise having a rule before a different one would make it different.
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster
    so i put 2 before 3?? @Socks
    Did you cheat and read the answer ? ;)


    . . . . . . . . . . . . .

    Lets imagine the actor's starting X is 317 (or whatever).

    1) You create a variable V with a default value of zero.

    V =0

    2) You then tell the actor to constrain its X position to this variable.

    V=0 . . . . so . . . X=0

    3) You then change the variable to the actor's X position.

    X=0 . . . so . . . V=0


    Everything will hang around 0 regardless of it's original X position.

    . . . . . . . . . . . . .

    Swap 2 and 3

    . . . . . . . . . . . . .

    Lets imagine the actor's starting X is 317 (or whatever).

    1) You create a variable V with a default value of zero.

    V =0

    2) You then change the variable to the actor's X position.

    X=317 . . . so . . . V=317

    3) You then tell the actor to constrain its X position to this variable.

    V=317 . . . . so . . . X=317




    Everything will hang around the actor's original X position.

    . . . . . . . . . . . . .


  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    thanks @Socks i now understand :) but its just something that doesnt quite add up. so iv done all of that and when i hit preview all the enemies that are placed all over the world arnt together, there "KIND OF" where they should be...

    so an example; i place an actor at position 372x then i place all the rules in its prototype, the enemy wont start at this point, nor return to it, it will start like at 300x or maybe 400, im not sure why? :(
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @CodeMonster

    Works fine for me, I stuck a red marker underneath some randomly place blocks to see that they were moving as they should be, works fine.

    Example attached:


    P.S . . . on a side note . . . . in the main actor switch off the bottom constrain behaviour - and switch on the two rules in the middle that are currently switched off (a change attribute action and a constrain behaviour) - I don't know how your game works, but having a random start position for each enemy looks better to me.
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    its very strange @Socks i copied exactly from your template, the random stuff didnt work for me, they all just spawned above eachother same x position, then wen i did the normal one where they all move the same way at the same time, they dont start at there x position like yours do, im sooo confused why is this happening to me??
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    would i be able to give you a copy of my game . 1.5mb and you can check it out maybe?
  • SocksSocks London, UK.Member Posts: 12,822
    would i be able to give you a copy of my game . 1.5mb and you can check it out maybe?
    Sure.
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    @Socks thanks i messaged you the details
  • SocksSocks London, UK.Member Posts: 12,822
    File doesn't work, when I double click an actor I can't see its rules, nothing comes up (must be a PC > Mac thing ?).

    Can you simply take a screenshot of the relevant rules.
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    ok ill do that, but if that happens then how am i going to publish my game to ios once its done, ah oh..........
Sign In or Register to comment.