Flash AS3 神秘闪烁 MovieClip

发布于 2024-09-03 01:14:35 字数 7304 浏览 4 评论 0原文

这是迄今为止我在 flash 中遇到的最奇怪的问题。我不知道是什么原因造成的。如果有人想实际查看它,我可以提供 .swf,但我会尽力描述它。

我正在为坦克物体创建子弹来射击。坦克是文档类的子类。我创建子弹的方式是:

varBullet:Bullet = new Bullet();
(parent as MovieClip).addChild(bullet);

子弹本身只是使用 this.x += 5; 之类的代码朝某个方向移动,

问题是子弹会跟踪它们的位置在正确的时间创建和销毁,但是子弹有时直到屏幕的一半才可见,有时根本不可见,有时在整个遍历过程中都可见。奇怪的是,删除我在子弹创建上的计时器似乎可以解决这个问题。

计时器的实现如下:

if(shot_timer == 0) {
射击(); // 这包含前面提到的项目符号创建方法
shot_timer = 10;

我的坦克对象的输入帧处理程序控制计时器,并在每帧大于零时将其递减。

谁能建议为什么会发生这种情况?

编辑:根据要求,完整代码:

Bullet.as

package {

import flash.display.MovieClip;
import flash.events.Event;

public class Bullet extends MovieClip {

    public var facing:int;
    private var speed:int;

    public function Bullet():void {
        trace("created");
        speed = 10;
        addEventListener(Event.ADDED_TO_STAGE,addedHandler);
    }

    private function addedHandler(e:Event):void {
        addEventListener(Event.ENTER_FRAME,enterFrameHandler);
        removeEventListener(Event.ADDED_TO_STAGE,addedHandler);
    }

    private function enterFrameHandler(e:Event):void {
        //0 - up, 1 - left, 2 - down, 3 - right
        if(this.x > 720 || this.x < 0 || this.y < 0 || this.y > 480) {
            removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
            trace("destroyed");
            (parent as MovieClip).removeChild(this);
            return;
        }

        switch(facing) {
            case 0: this.y -= speed; break;
            case 1: this.x -= speed; break;
            case 2: this.y += speed; break;
            case 3: this.x += speed; break;
        }
    }
}

}

Tank.as:

package {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;

public class Tank extends MovieClip {

    private var right:Boolean = false;
    private var left:Boolean = false;
    private var up:Boolean = false;
    private var down:Boolean = false;
    private var facing:int = 0; //0 - up, 1 - left, 2 - down, 3 - right
    private var horAllowed:Boolean = true;
    private var vertAllowed:Boolean = true;
    private const GRID_SIZE:int = 100;
    private var shooting:Boolean = false;
    private var shot_timer:int = 0;

    private var speed:int = 2;

    public function Tank():void {
        addEventListener(Event.ADDED_TO_STAGE,stageAddHandler);
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    private function stageAddHandler(e:Event):void {
        stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeys);
        stage.addEventListener(KeyboardEvent.KEY_UP,keyUps);
        removeEventListener(Event.ADDED_TO_STAGE,stageAddHandler);
    }

    public function checkKeys(event:KeyboardEvent):void {
        if(event.keyCode == 32) {
            //trace("Spacebar is down");
            shooting = true;
        }
        if(event.keyCode == 39) {
            //trace("Right key is down");
            right = true;
        }
        if(event.keyCode == 38) {
            //trace("Up key is down");  // lol
            up = true;
        }
        if(event.keyCode == 37) {
            //trace("Left key is down");
            left = true;
        }
        if(event.keyCode == 40) {
            //trace("Down key is down");
            down = true;
        }
    }

    public function keyUps(event:KeyboardEvent):void {
        if(event.keyCode == 32) {
            event.keyCode = 0;
            shooting = false;
            //trace("Spacebar is not down");
        }
        if(event.keyCode == 39) {
            event.keyCode = 0;
            right = false;
            //trace("Right key is not down");
        }
        if(event.keyCode == 38) {
            event.keyCode = 0;
            up = false;
            //trace("Up key is not down");
        }
        if(event.keyCode == 37) {
            event.keyCode = 0;
            left = false;
            //trace("Left key is not down");
        }
        if(event.keyCode == 40) {
            event.keyCode = 0;
            down = false;
            //trace("Down key is not down") // O.o
        }
    }

    public function checkDirectionPermissions(): void {
        if(this.y % GRID_SIZE < 5 || GRID_SIZE - this.y % GRID_SIZE < 5) {
            horAllowed = true;
        } else {
            horAllowed = false;
        }
        if(this.x % GRID_SIZE < 5 || GRID_SIZE - this.x % GRID_SIZE < 5) {
            vertAllowed = true;
        } else {
            vertAllowed = false;
        }

        if(!horAllowed && !vertAllowed) {
            realign();
        }
    }

    public function realign():void {
        if(!horAllowed) {
            if(this.x % GRID_SIZE < GRID_SIZE / 2) {
                this.x -= this.x % GRID_SIZE;
            } else {
                this.x += (GRID_SIZE - this.x % GRID_SIZE);
            }   
        }
        if(!vertAllowed) {
            if(this.y % GRID_SIZE < GRID_SIZE / 2) {
                this.y -= this.y % GRID_SIZE;
            } else {
                this.y += (GRID_SIZE - this.y % GRID_SIZE);
            }
        }
    }

    public function enterFrameHandler(Event):void {
        //trace(shot_timer);
        if(shot_timer > 0) {
            shot_timer--;
        }
        movement();
        firing();
    }

    public function firing():void {
        if(shooting) {
            if(shot_timer == 0) {
                shoot();
                shot_timer = 10;
            }
        }
    }

    public function shoot():void {
        var bullet = new Bullet();
        bullet.facing = facing;
        //0 - up, 1 - left, 2 - down, 3 - right
        switch(facing) {
            case 0: bullet.x = this.x; bullet.y = this.y - this.height / 2; break;
            case 1: bullet.x = this.x - this.width / 2; bullet.y = this.y; break;
            case 2: bullet.x = this.x; bullet.y = this.y + this.height / 2; break;
            case 3: bullet.x = this.x + this.width / 2; bullet.y = this.y; break;
        }
        (parent as MovieClip).addChild(bullet);
    }

    public function movement():void {
        //0 - up, 1 - left, 2 - down, 3 - right

        checkDirectionPermissions();

        if(horAllowed) {
            if(right) {
                orient(3);
                realign();
                this.x += speed;
            }
            if(left) {
                orient(1);
                realign();
                this.x -= speed;
            }
        }
        if(vertAllowed) {
            if(up) {
                orient(0);
                realign();
                this.y -= speed;
            }
            if(down) {
                orient(2);
                realign();
                this.y += speed;
            }
        }
    }

    public function orient(dest:int):void {
        //trace("facing: " + facing);
        //trace("dest: " + dest);
        var angle = facing - dest;
        this.rotation += (90 * angle);
        facing = dest;
    }

}

}

如果有人想尝试复制错误,完整的应用程序就在这里。它的部分内容使用 electroserver,因此可能必须将它们注释掉。

TankAttack.rar

This is the strangest problem I've faced in flash so far. I have no idea what's causing it. I can provide a .swf if someone wants to actually see it, but I'll describe it as best I can.

I'm creating bullets for a tank object to shoot. The tank is a child of the document class. The way I am creating the bullet is:

var bullet:Bullet = new Bullet();
(parent as MovieClip).addChild(bullet);

The bullet itself simply moves itself in a direction using code like this.x += 5;

The problem is the bullets will trace for their creation and destruction at the correct times, however the bullet is sometimes not visible until half way across the screen, sometimes not at all, and sometimes for the whole traversal. Oddly removing the timer I have on bullet creation seems to solve this.

The timer is implemented as such:

if(shot_timer == 0) {
shoot(); // This contains the aforementioned bullet creation method
shot_timer = 10;

My enter frame handler for the tank object controls the timer and decrements it every frame if it is greater than zero.

Can anyone suggest why this could be happening?

EDIT: As requested, full code:

Bullet.as

package {

import flash.display.MovieClip;
import flash.events.Event;

public class Bullet extends MovieClip {

    public var facing:int;
    private var speed:int;

    public function Bullet():void {
        trace("created");
        speed = 10;
        addEventListener(Event.ADDED_TO_STAGE,addedHandler);
    }

    private function addedHandler(e:Event):void {
        addEventListener(Event.ENTER_FRAME,enterFrameHandler);
        removeEventListener(Event.ADDED_TO_STAGE,addedHandler);
    }

    private function enterFrameHandler(e:Event):void {
        //0 - up, 1 - left, 2 - down, 3 - right
        if(this.x > 720 || this.x < 0 || this.y < 0 || this.y > 480) {
            removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
            trace("destroyed");
            (parent as MovieClip).removeChild(this);
            return;
        }

        switch(facing) {
            case 0: this.y -= speed; break;
            case 1: this.x -= speed; break;
            case 2: this.y += speed; break;
            case 3: this.x += speed; break;
        }
    }
}

}

Tank.as:

package {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;

public class Tank extends MovieClip {

    private var right:Boolean = false;
    private var left:Boolean = false;
    private var up:Boolean = false;
    private var down:Boolean = false;
    private var facing:int = 0; //0 - up, 1 - left, 2 - down, 3 - right
    private var horAllowed:Boolean = true;
    private var vertAllowed:Boolean = true;
    private const GRID_SIZE:int = 100;
    private var shooting:Boolean = false;
    private var shot_timer:int = 0;

    private var speed:int = 2;

    public function Tank():void {
        addEventListener(Event.ADDED_TO_STAGE,stageAddHandler);
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    private function stageAddHandler(e:Event):void {
        stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeys);
        stage.addEventListener(KeyboardEvent.KEY_UP,keyUps);
        removeEventListener(Event.ADDED_TO_STAGE,stageAddHandler);
    }

    public function checkKeys(event:KeyboardEvent):void {
        if(event.keyCode == 32) {
            //trace("Spacebar is down");
            shooting = true;
        }
        if(event.keyCode == 39) {
            //trace("Right key is down");
            right = true;
        }
        if(event.keyCode == 38) {
            //trace("Up key is down");  // lol
            up = true;
        }
        if(event.keyCode == 37) {
            //trace("Left key is down");
            left = true;
        }
        if(event.keyCode == 40) {
            //trace("Down key is down");
            down = true;
        }
    }

    public function keyUps(event:KeyboardEvent):void {
        if(event.keyCode == 32) {
            event.keyCode = 0;
            shooting = false;
            //trace("Spacebar is not down");
        }
        if(event.keyCode == 39) {
            event.keyCode = 0;
            right = false;
            //trace("Right key is not down");
        }
        if(event.keyCode == 38) {
            event.keyCode = 0;
            up = false;
            //trace("Up key is not down");
        }
        if(event.keyCode == 37) {
            event.keyCode = 0;
            left = false;
            //trace("Left key is not down");
        }
        if(event.keyCode == 40) {
            event.keyCode = 0;
            down = false;
            //trace("Down key is not down") // O.o
        }
    }

    public function checkDirectionPermissions(): void {
        if(this.y % GRID_SIZE < 5 || GRID_SIZE - this.y % GRID_SIZE < 5) {
            horAllowed = true;
        } else {
            horAllowed = false;
        }
        if(this.x % GRID_SIZE < 5 || GRID_SIZE - this.x % GRID_SIZE < 5) {
            vertAllowed = true;
        } else {
            vertAllowed = false;
        }

        if(!horAllowed && !vertAllowed) {
            realign();
        }
    }

    public function realign():void {
        if(!horAllowed) {
            if(this.x % GRID_SIZE < GRID_SIZE / 2) {
                this.x -= this.x % GRID_SIZE;
            } else {
                this.x += (GRID_SIZE - this.x % GRID_SIZE);
            }   
        }
        if(!vertAllowed) {
            if(this.y % GRID_SIZE < GRID_SIZE / 2) {
                this.y -= this.y % GRID_SIZE;
            } else {
                this.y += (GRID_SIZE - this.y % GRID_SIZE);
            }
        }
    }

    public function enterFrameHandler(Event):void {
        //trace(shot_timer);
        if(shot_timer > 0) {
            shot_timer--;
        }
        movement();
        firing();
    }

    public function firing():void {
        if(shooting) {
            if(shot_timer == 0) {
                shoot();
                shot_timer = 10;
            }
        }
    }

    public function shoot():void {
        var bullet = new Bullet();
        bullet.facing = facing;
        //0 - up, 1 - left, 2 - down, 3 - right
        switch(facing) {
            case 0: bullet.x = this.x; bullet.y = this.y - this.height / 2; break;
            case 1: bullet.x = this.x - this.width / 2; bullet.y = this.y; break;
            case 2: bullet.x = this.x; bullet.y = this.y + this.height / 2; break;
            case 3: bullet.x = this.x + this.width / 2; bullet.y = this.y; break;
        }
        (parent as MovieClip).addChild(bullet);
    }

    public function movement():void {
        //0 - up, 1 - left, 2 - down, 3 - right

        checkDirectionPermissions();

        if(horAllowed) {
            if(right) {
                orient(3);
                realign();
                this.x += speed;
            }
            if(left) {
                orient(1);
                realign();
                this.x -= speed;
            }
        }
        if(vertAllowed) {
            if(up) {
                orient(0);
                realign();
                this.y -= speed;
            }
            if(down) {
                orient(2);
                realign();
                this.y += speed;
            }
        }
    }

    public function orient(dest:int):void {
        //trace("facing: " + facing);
        //trace("dest: " + dest);
        var angle = facing - dest;
        this.rotation += (90 * angle);
        facing = dest;
    }

}

}

The complete application is here if someone wants to attempt to replicate the error. It uses electroserver for parts of it so they may have to be commented out.

TankAttack.rar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自演自醉 2024-09-10 01:14:35

这些线正确吗?

public function enterFrameHandler(Event):void { //Instead of e:Event

var bullet = new Bullet(); // Instead of var bullet:Bullet

var angle = facing - dest; // Instead of var angle:int

只是想确保我读得正确。

Are these lines correct ?

public function enterFrameHandler(Event):void { //Instead of e:Event

,

var bullet = new Bullet(); // Instead of var bullet:Bullet

And

var angle = facing - dest; // Instead of var angle:int

I just want to make sure I am reading this right.

盛夏已如深秋| 2024-09-10 01:14:35

我认为这个问题的答案非常简单,这种情况我已经发生过很多次了(CS4)。检查你的时间...通常会有一个额外的不需要的空帧。这种情况是如何发生的对我来说仍然是一个谜,但这通常是罪魁祸首......无意中按下了某个键。

I think the answer to this is really simple, I had this happen to me a number of times (CS4). Check your timelime... there's usually an extra unwanted empty frame. How this happens is still a bit of a mystery to me, but it's usually the culprit... from hitting some key inadvertently.

扮仙女 2024-09-10 01:14:35

将方法 stop(); 添加到项目符号中。有时,当影片剪辑重复播放时,它会闪烁。

Add the method stop(); to the bullet. Sometimes it flickers when the movie clip repeats.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文