Erstellen Sie ein Objekt in Actionscript, die 2D-Bewegung hat und stoppt, wenn sie eine Wand trifft

StackOverflow https://stackoverflow.com/questions/1370725

Frage

Ich möchte einen Platz machen, die Tastatur Bewegung hat (oben, unten, links, rechts) und stoppt, wenn es ein anderes Objekt, wie eine Wand trifft.

EDIT: Ich habe bereits ein Quadrat und ein Tastaturlayout, aber wenn das muss etwas Bestimmtes sein, dann sagen Sie mir bitte

War es hilfreich?

Lösung

jackson, alles, was Sie tun müssen, ist

  1. für Tasten Hören
  2. aktualisieren Sie Ihren Charakter
  3. Check für colissions

Sie sind nicht spezifisch beeing, aber ich bin 100% sicher, dass wenn Sie ein bisschen googeln mehr in das, was Sie brauchen Sie es zu finden, da es Tonnen von Flash-Gaming-Tutorials sind.

Hier ist ein minimales Setup

//needed to update the position
var velocityX:Number = 0;
var velocityY:Number = 0;
//draw the ball
var ball:Sprite = new Sprite();
ball.graphics.beginFill(0);
ball.graphics.drawCircle(0,0,20);
ball.graphics.endFill();
addChild(ball);
ball.x = ball.y = 100;
//setup keys
stage.addEventListener(KeyboardEvent.KEY_DOWN, updateBall);
function updateBall(event:KeyboardEvent):void{
    switch(event.keyCode){
        case Keyboard.RIGHT:
        if(velocityX < 6) velocityX += .25;
        break;
        case Keyboard.LEFT:
        if(velocityX > -6) velocityX -= .25;
        break;
        case Keyboard.DOWN:
        if(velocityY < 6) velocityY += .25;
        break;
        case Keyboard.UP:
        if(velocityY > -6) velocityY -= .25;
        break;
    }
    //update ball position
    ball.x += velocityX;
    ball.y += velocityY;
    //check walls , if collision, flip direction
    if(ball.x > stage.stageWidth || ball.x < 0) velocityX *= -1;
    if(ball.y > stage.stageHeight|| ball.y < 0) velocityY *= -1;
}

oviously ist es nicht ideal, aber es ist einfach und es zeigt die Punkte Staaten an der Spitze leicht. Sie könnten einige glatte Tasten verwenden möchten und Ihr Spiel onEnterFrame aktualisieren.

Goodluck

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top