Hello
I have an issue with my characters movement
Im sure some of you must have come across the issue.
Below is the code I use to make my character move, pretty simple, right?
The only problem is, is that he moves diagonally faster then normal.
Normal speed is 2
Diagonal speed = root(sqrt(2)+sqrt(2))
= 2.82...
So how could I edit my code to make him move spd 2 in all directions?
Code: package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent
import flash.ui.Keyboard
import flash.events.Event
public class Player extends MovieClip
{
var upkeypressed:Boolean = false
var leftkeypressed:Boolean = false
var downkeypressed:Boolean = false
var rightkeypressed:Boolean = false
var spd:Number = 3
public function Player()
{
addEventListener(Event.ADDED_TO_STAGE,addedtostage,false,0,true)
}
function addedtostage(e:Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown,false,0,true)
stage.addEventListener(KeyboardEvent.KEY_UP,keyup,false,0,true)
stage.addEventListener(Event.ENTER_FRAME,step,false,0,true)
}
function step(e:Event):void
{
if (upkeypressed)
{
y-=spd
}
if (leftkeypressed)
{
x-=spd
}
if (downkeypressed)
{
y+=spd
}
if (rightkeypressed)
{
x+=spd
}
}
function keydown(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case Keyboard.UP:
upkeypressed = true
break;
case Keyboard.LEFT:
leftkeypressed = true
break;
case Keyboard.DOWN:
downkeypressed = true
break;
case Keyboard.RIGHT:
rightkeypressed = true
break;
}
}
function keyup(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case Keyboard.UP:
upkeypressed = false
break;
case Keyboard.LEFT:
leftkeypressed = false
break;
case Keyboard.DOWN:
downkeypressed = false
break;
case Keyboard.RIGHT:
rightkeypressed = false
break;
}
}
}
}
[AS3] Diagonal Movement
Friday, May 11, 2012
[Action Script] [AS3] Diagonal Movement May,2012
Posted by Bimo Hery Prabowo at 11:13 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment