【问题标题】:How to stopping moving object?如何停止移动的物体?
【发布时间】:2013-11-09 21:12:31
【问题描述】:

我有 Tank 的课程,当它被调用时它正在移动,但是这个 Tank 根本没有停止。我希望这辆坦克在它到达某个位置(位置)时停下来..

 package com.musuh 
 {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;

public class Tank extends MovieClip
{
    private var dx:Number; // speed and direction
    private var lastTime:int; // animation time
    private var side:String;
    private var pos:Number;
    private var speed:Number=2;

    public function Tank(side:String, pos:int, altitude:Number)
    {
        this.side =side;
        this.pos = pos;

        if (side == "left") {
            this.x = 20; // start to the left
            dx = speed; // fly left to right
            this.scaleX = 1; // reverse
        } else if (side == "right") {
            this.x = 1200; // start to the right
            dx = -speed; // fly right to left
            this.scaleX = -1; // not reverse
        }
        this.y = altitude; // vertical position

        addEventListener(Event.ENTER_FRAME,moveTank);
        lastTime = getTimer();

    }

    public function moveTank(event:Event) {
        var timePassed:int = getTimer()-lastTime;
        lastTime += timePassed;

        // move Tank
        this.x += dx*timePassed/1000;
        this.y += 1;

        if (this.x == pos)
        {
            this.x = pos;
        }

    }
}

}

当 this.x 到达 pos =>> 它不会停止! , 为什么??

【问题讨论】:

  • 你的 x 可以超过 pos ,所以检查它像:if (this.x >= pos)

标签: actionscript-3 flash actionscript flash-cs5


【解决方案1】:

你在哪里声明'pos'?

你应该检查你的输入框架,如果它超过了某个数字,那么,你会得到这个,假设 pos 有一个值:

public function moveTank(event:Event) {
        var timePassed:int = getTimer()-lastTime;
        lastTime += timePassed;

        // move Tank
        this.x += dx*timePassed/1000;
        this.y += 1;

        if (this.x > pos)
        {
            this.x = pos;
        }



    }

进入帧检查每一帧发生了什么。您正在为每一帧的坦克对象添加一些值,例如坦克每帧移动 1 个像素。如果更新的位置超过了 pos 变量的值,if 语句会检查每一帧。

祝你好运。

【讨论】:

    【解决方案2】:

    先在moveTank()中有this.x的值后添加trace语句

    this.x += dx*timePassed/1000;
    trace (this.x);
    this.y += 1;`  
    

    您会注意到,在 this.x 值与 pos 值相交的点上,它们并不相等。 所以你的 (this.x == pos) 条件失败。 因此,您应该始终检查

    if (this.x >= pos)
    {
       this.x = pos;
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多