Note that there are some explanatory texts on larger screens.

plurals
  1. POCollision and movements not working as expected
    primarykey
    data
    text
    <p>I am trying to do an example of collision in Action Script 3. It's a <code>character</code> that should stop when it hits a <code>platform</code>. It works well when I move only to the right, left, up or down directions, but if I try to move in the diagonals, if the <code>character</code>is colliding with the <code>platform</code>, the object goes to a different area of the screen.</p> <p>This is the compiled example: <a href="http://dl.dropbox.com/u/5282142/GameDemo.html" rel="nofollow">http://dl.dropbox.com/u/5282142/GameDemo.html</a></p> <p>And below is my code.</p> <p>Now, does anyone know a better way to do what I am doing, or how can I get the character not to go to a weird position when I try to move it in a diagonal?</p> <pre><code>var level:Array = new Array(); for (var i = 0; i &lt; numChildren; i++) { if (getChildAt(i) is Platform) { level.push(getChildAt(i).getBounds(this)); } } var speedX:int = 0; var speedY:int = 0; var kLeft:Boolean = false; var kRight:Boolean = false; var kDown:Boolean = false; var kUp:Boolean = false; stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler); function onKeyDownHandler(event:KeyboardEvent):void { if (event.keyCode == 37) kLeft = true; if (event.keyCode == 38) kUp = true; if (event.keyCode == 39) kRight = true; if (event.keyCode == 40) kDown = true; } function onKeyUpHandler(event:KeyboardEvent):void { if (event.keyCode == 37) kLeft = false; if (event.keyCode == 38) kUp = false; if (event.keyCode == 39) kRight = false; if (event.keyCode == 40) kDown = false; } addEventListener(Event.ENTER_FRAME, loop); function loop(event:Event):void { moveChar(); bound(); } function moveChar():void { if (kLeft) { speedX = -10; } else if (kRight) { speedX = 10; } else { speedX *= 0.5; } if (kUp) { speedY = -10; } else if (kDown) { speedY = 10; } else { speedY *= 0.5; } character.x += speedX; character.y += speedY; } function bound():void { if (character.x &gt; (800 - character.width/2)){ character.x = 800 - character.width/2; } if (character.x &lt; (character.width/2)){ character.x = character.width/2; } if (character.y &gt; (480 - character.height/2)){ character.y = 480 - character.height/2; } if (character.y &lt; (character.height/2)){ character.y = character.height/2; } for (i = 0; i &lt; level.length; i++) { if (character.getBounds(this).intersects(level[i])) { if (speedX &gt; 0) { character.x = level[i].left - character.width/2; } if (speedX &lt; 0) { character.x = level[i].right + character.width/2; } } } for (i = 0; i &lt; level.length; i++) { if (character.getBounds(this).intersects(level[i])) { if (speedY &gt; 0) { character.y = level[i].top - character.height/2; } if (speedY &lt; 0) { character.y = level[i].bottom + character.height/2; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload