遇到问题朝着AS3字符 [英] Having trouble moving a character in AS3

查看:115
本文介绍了遇到问题朝着AS3字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有点新的ActionScript 3,如果我想的东西至关重要的解决只是告诉我,我将它张贴,但反正...

So I'm a little new to Actionscript 3 and if I'm missing something crucial to solving just tell me and I will post it, but anyways...

所以,我有2层。最上层是内容和底层是AS3(对于动作)。在我的内容我有一点点的蓝色球是约。在舞台的中心。而在我的AS3层,我有以下code:

So I have 2 layers. The top layer is content and the bottom layer is as3 (for actionscript). In my content I have a little blue ball that is approx. in the center of the stage. And in my as3 layer I have the following code:

//Add the event listeners...
stage.addEventListener(Event.ENTER_FRAME, moveBall);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyrelease);

//Movement variables...
var velY;
var velX;
var power = 5;
var friction = 0.95;

//Key variables...
var right;
var left;
var up;
var down;

function keyrelease(event:KeyboardEvent) {
    right = false;
    left = false;
    down = false;
    up = false;
}

function keypress(event:KeyboardEvent) {
    if (event.keyCode == 39) {
        right = true;
    }
    if (event.keyCode == 37) {
        left = true;
    }
    if (event.keyCode == 38) {
        up = true;
    }
    if (event.keyCode == 40) {
        down = true;
    }
}

function moveBall(event:Event) {
    if (right == true) {
        velX += power;
    }
    if (left == true) {
        velX -= power;
    }
    if (up == true) {
        velY += power;
    }
    if (down == true) {
        velY -= power;
    }

    character.x += velX;
    character.y += velY;


    velY *= friction;
    velX *= friction;
}

在哪里基本上是我做的是检查的关键是pressed,如果让我做velY或velX等于鲍威,这将增加蓝色小球以一定的方式和velY和velX将继续下降,直到(因为舍入误差)就变成零和蓝色球停。但是,没有什么工作的钥匙,而由于某种原因,我的小蓝圈是在屏幕的上方最左边的角落。

Where basically what I am doing is check if a key is pressed and if so I make velY or velX equal powe that will increment the little blue ball a certain way and velY and velX will keep decreasing until (because of rounding errors) it becomes zero and the blue ball stops. But, nothing is working with the keys, and for some reason my little blue circle is in the upper left most corner of the screen.

推荐答案

那是因为你没有初始化速度变量。本是无类型的,因此它们的初始值是未定义

That is because you don't initialize the velocity variables. The are untyped, thus their initial value is undefined.

在这些线路会自动提升为

In these lines they are automatically promoted to Number

character.x += velX;
character.y += velY;

在强迫未定义变成 NaN的 (非数字)。因此,所产生的坐标 NaN的。 FlashPlayer的通过将的DisplayObject 0回应了这一点。

When coercing undefined to Number it becomes NaN (not a number). Thus the resulting coordinates are NaN. FlashPlayer responds to this by putting the DisplayObject to 0.

另外,我建议使用 flash.geom.Point (良好操作坐标), flash.ui.Keyboard (远远更具可读性)和 flash.utils.Dictionary (而不是有许多标志和IFS,建立查找地图)。

Also, I'd suggest using flash.geom.Point (good to manipulate coordinates), flash.ui.Keyboard (is far more readable) and flash.utils.Dictionary (rather than having many flags and ifs, build lookup maps).

//Add the event listeners...
stage.addEventListener(Event.ENTER_FRAME, moveBall);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyrelease);

//Movement variables...
const power:Number = 5;
const friction:Number = 0.95;
var velocity:Point = new Point();

var map:Dictionary = new Dictionary();
map[Keyboard.LEFT] = new Point(-power, 0);
map[Keyboard.UP] = new Point(0, -power);
map[Keyboard.RIGHT] = new Point(power, 0);
map[Keyboard.DOWN] = new Point(0, -power);

var acceleration:Dictionary = new Dictionary();

function keyrelease(event:KeyboardEvent):void {
    delete acceleration[map[event.keyCode]];
}

function keypress(event:KeyboardEvent):void {
    var p:Point = map[event.keyCode];
    if (p) acceleration[p] = p;
}

function moveBall(event:Event):void {
    for each (var p:Point in acceleration) velocity = velocity.add(p);
    character.x += velocity.x;
    character.y += velosity.y;
    velocity.normalize(velocity.length * friction);
}

这篇关于遇到问题朝着AS3字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆