0

每次 object(1).y 超出舞台时,我都会尝试随机化一个 object(2)。但问题来了,它不断移动,有时它“跳跃”到我想要做出改变的那个位置。我确实尝试过,"if (road1.y >= stage.stageHeight) {"但它没有触发。而当我这样做时,它只会在它之前在舞台上 2 次时触发它的运动速度。所有影片剪辑的注册点都在左上角。代码是这样的

private var road1,road4:Road1;
    private var road2:Road2;
    private var road3:Road3;
    private var randomRoad:Sprite = new Sprite();
    private var offset:Number = 0;
    private var counter:Number = 0;


public function onAdded(e:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE,onAdded);
        addChild(road1=new Road1());
        addChild(randomRoad);
        addChild(road4=new Road1());
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    public function onEnterFrame(e:Event):void {


        if (startRandom == true) {
            if (Math.random() > 0.5) {
                randomRoad.addChild(road2 =new Road2());
            } else {
                randomRoad.addChild(road3 =new Road3());
            }
            startRandom = false;
            trace(randomRoad);
            trace(startRandom);
        }

        if (road1.y >= stage.stageHeight) {
            startRandom = true;
            trace("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
        }

        offset +=  speed;
        counter++;
        road1.y = offset % 800 - 800;
        randomRoad.y = road1.y + road1.height;
        road4.y = randomRoad.y + randomRoad.height;

    }
4

3 回答 3

0

舞台高度是多少?800?也许问题在于 road1.y 永远不会大于 -1;

于 2013-08-28T13:27:11.303 回答
0

Wouldn't it be more logical to base your spawning of an object on distance traveled? Then it wouldn't be reliant on determining the y position of a given road piece in relation to the screen.

I am going to assume you have a car in this game, since there is road. Give the car a distance variable and increment that variable based on the speed.

distance += speed;
if (distance > 400)
{
    spawnObject();
    distance = 0; // reset the distance traveled.
}

EDIT : I think I may have misunderstood what you were trying to do as I thought you were trying to spawn objects on the side of the road and this was a method of determining when to spawn them. In re-reading it, it seems like the 'object' you are trying to spawn is the next road piece itself. Would have been better to just use the word road in your question as opposed to 'object(2)'

于 2013-08-29T04:35:38.050 回答
0

在此处输入图像描述

尝试这个:

if (road1.y >= -speed) {
    startRandom = true;
    trace("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
}
于 2013-08-28T18:18:20.813 回答