Quote Nikag:
- Code: Select All Code
var timer:Timer;
var direct:String;
initStage();
function initStage() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,startMove);
}
function startMove(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.RIGHT:
direct = "right";
break;
case Keyboard.LEFT:
direct = "left";
break;
case Keyboard.DOWN:
direct = "down";
break;
case Keyboard.UP:
direct = "up";
}
timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, moveCar);
timer.start();
stage.removeEventListener(KeyboardEvent.KEY_DOWN, startMove);
stage.addEventListener(KeyboardEvent.KEY_UP, stopMove);
}
function stopMove(e:KeyboardEvent):void {
timer.stop();
initStage();
}
function moveCar(e:TimerEvent):void {
switch (direct) {
case "right":
this.x -= 5;
hero1.x+=5;
break;
case "left":
this.x += 5;
hero1.x-=5;
break;
case "up":
this.y -= 5;
break;
case "down":
this.y += 5;
break;
}
}
... Why so much code, it is just ridiculous. My (experimental) solution for what you got is this:
Click to Play
(Javascript Required)
Rolling ball.swf [ 1.75 KiB | Viewed 2127 times ]
This is the code that is used in the ball (AS2) and it is also the only piece of code that is used in the document.
- Code: Select All Code
onClipEvent (enterFrame) {
speed = 3;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.RIGHT)) {
_root.ball._x += speed;
_root.ball._rotation += speed;
}
if (Key.isDown (Key.LEFT)) {
_root.ball._x -= speed;
_root.ball._rotation -= speed;
}
if (Key.isDown (Key.RIGHT) && Key.isDown (Key.SHIFT)) {
_root.ball._x += speed*1.4;
_root.ball._rotation += speed*1.4;
}
if (Key.isDown (Key.LEFT) && Key.isDown (Key.SHIFT)) {
_root.ball._x -= speed*1.4;
_root.ball._rotation -= speed*1.4;
}
}
The first half of the code makes me able to move the ball and the other half makes me able to speed it up if I hold down shift so that means even if i add a few more things to it, it still is less code. I don't know if it is AS3 that makes things so much more complicated or if it just is the way you made it.
Also, you can press any key on the keyboard (I believe) and it moves in the same direction as you pressed.