Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 当子弹电影剪辑进入舞台时,敌人和子弹电影剪辑冻结-AS3-CS6_Actionscript 3_Flash_Movieclip_Flash Cs6 - Fatal编程技术网

Actionscript 3 当子弹电影剪辑进入舞台时,敌人和子弹电影剪辑冻结-AS3-CS6

Actionscript 3 当子弹电影剪辑进入舞台时,敌人和子弹电影剪辑冻结-AS3-CS6,actionscript-3,flash,movieclip,flash-cs6,Actionscript 3,Flash,Movieclip,Flash Cs6,我正在为大学作业做一个自上而下的射击。当我按下鼠标左键射击子弹时,它们会在与玩家电影剪辑后面的敌人电影剪辑合并时冻结,当这种情况发生时,玩家电影剪辑仍然可以使用箭头键移动,因此SWF不会崩溃 敌人和子弹电影剪辑被放置在一个阵列中,并且在相互碰撞时都应该被移除 在输出窗口中,当bullet电影剪辑进入舞台时,我收到此错误消息 子弹击中坏人0 TypeError:错误#1034:类型强制失败:无法转换闪存。显示::Stage@2862e041要闪烁.display.MovieClip 在Bullet

我正在为大学作业做一个自上而下的射击。当我按下鼠标左键射击子弹时,它们会在与玩家电影剪辑后面的敌人电影剪辑合并时冻结,当这种情况发生时,玩家电影剪辑仍然可以使用箭头键移动,因此SWF不会崩溃

敌人和子弹电影剪辑被放置在一个阵列中,并且在相互碰撞时都应该被移除

在输出窗口中,当bullet电影剪辑进入舞台时,我收到此错误消息

子弹击中坏人0

TypeError:错误#1034:类型强制失败:无法转换闪存。显示::Stage@2862e041要闪烁.display.MovieClip

在Bullet/removeSelf()处

在Bullet/loop()处

在主/环路()处

子弹命中的恶棍0
之所以存在,是因为它在代码中被跟踪

Main.as

package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends MovieClip
{
    public var player:Player;
    public var enemy:Enemy;

    public var bulletList:Array = [];

    public var mousePressed:Boolean = false; //keeps track of whether the mouse is currently pressed down
    public var delayCounter:int = 0; //this adds delay between the shots
    public var delayMax:int = 7; //change this number to shoot more or less rapidly
     
    var enemies:Array =  [];

    public function Main():void
    {
        player = new Player(stage, 320, 240);
        stage.addChild(player);

        //stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true); //remove this
        stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
        stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);

        stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
        for(var numBaddies=0; numBaddies<6;numBaddies++){
            var enemy:Enemy = new Enemy(null);
            enemy.x = numBaddies*50;
            enemy.y = numBaddies*50
            stage.addChild(enemy);
            enemies.push(enemy);
        }
    }

    public function loop(e:Event):void
    {
        if(mousePressed) // as long as the mouse is pressed...
        {
            delayCounter++; //increase the delayCounter by 1
            if(delayCounter == delayMax) //if it reaches the max...
            {
                shootBullet(); //shoot a bullet
                delayCounter = 0; //reset the delay counter so there is a pause between bullets
                
           }
        }

        if(bulletList.length > 0)
        {
            for(var i:int = bulletList.length-1; i >= 0; i--)
            {
                bulletList[i].loop();
            }
        }
        
        for(var h = 0; h<bulletList.length; ++h)
        {
            if(bulletList[h].hitTestObject(this)){
              trace("player hit by baddie " + h);
               }
        }
        
        for(var u:int=0; u<enemies.length; u++) {
        Enemy(enemies[u]).moveTowards(player.x, player.y);
        }
    }
    

    public function mouseDownHandler(e:MouseEvent):void //add this function
    {
        mousePressed = true; //set mousePressed to true
    }

    public function mouseUpHandler(e:MouseEvent):void //add this function
    {
        mousePressed = false; //reset this to false
    }

    public function shootBullet():void //delete the "e:MouseEvent" parameter
    {
        var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation, enemies);
        bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true);
        bulletList.push(bullet);
        stage.addChild(bullet);
    }

    public function bulletRemoved(e:Event):void
    {
        e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved);
        bulletList.splice(bulletList.indexOf(e.currentTarget),1);
    }
}
}
package  {

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

public class Enemy extends MovieClip {
    
    public var bullets:Array;
    public var stageRef:Stage;

    private var enemyPositionX, enemyPositionY,xDistance,yDistance,myRotation:int;
    public function Enemy(bulletList:Array) {
        // constructor code
        bullets = bulletList;
    }
    
    public function moveTowards(playerX:int, playerY:int){
        xDistance = this.x - playerX;
        yDistance = this.y - playerY;


        myRotation = Math.atan2(yDistance, xDistance);

        this.x -= 3 * Math.cos(myRotation);
        this.y -= 3 * Math.sin(myRotation);
        
        
    }

    public function loopE():void{
    
    for(var i=0; i<bullets.length; ++i)
        {
            if(bullets[i].hitTestObject(this)){
               trace("you killed enemy " + i);
               removeSelf();
               }
        }
    }
    
    private function removeSelf()
    {
        removeEventListener(Event.ENTER_FRAME, loopE);
        if (stageRef.contains(this))
        stageRef.removeChild(this);
    }
}

}
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;

public class Bullet extends MovieClip
{
    private var stageRef:Stage; //checks if the bullet leaves the screen borders
    private var speed:Number = 10; //speed that the bullet will travel at
    private var xVel:Number = 0; //current x velocity
    private var yVel:Number = 0; //current y velocity
    private var rotationInRadians = 0; //convenient to store our rotation in radians instead of degrees
    private var allBaddies:Array;
    //our constructor requires: the stage, the position of the bullet, and the direction the bullet should be facing
    public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number, enemies:Array):void
    {
        this.stageRef = stageRef;
        this.x = X;
        this.y = Y;
        this.rotation = rotationInDegrees;
        this.rotationInRadians = rotationInDegrees * Math.PI / 180; //convert degrees to radians, for trigonometry
        allBaddies = enemies;
    }
    
    public function loop():void //we don't need to include the "e:Event" because we aren't using an EventListener
    {
        for(var b=0; b<allBaddies.length; ++b)
        {
            if(allBaddies[b].hitTestObject(this)){
               trace("bullet hit baddie " + b);
               removeSelf();
               }
        }
        
        xVel = Math.cos(rotationInRadians) * speed; //uses the cosine to get the xVel from the speed and rotation
        yVel = Math.sin(rotationInRadians) * speed; //uses the sine to get the yVel

        x += xVel; //updates the position
        y += yVel;

        //if the bullet goes off the edge of the screen...
        if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
        {
            this.parent.removeChild(this); //remove the bullet
        }
    }
    
    private function removeSelf()
    {
        removeEventListener(Event.ENTER_FRAME, loop);
        if (MovieClip(root).stageRef.contains(this))
        MovieClip(root).stageRef.removeChild(this);
    }
}
}
包
{
导入flash.display.Stage;
导入flash.display.MovieClip;
导入flash.events.Event;
导入flash.events.MouseEvent;
公共类Main扩展了MovieClip
{
公共玩家:玩家;
公敌:敌人;
公共变量公告列表:数组=[];
public var mousePressed:Boolean=false;//跟踪当前是否按下鼠标
public var delayCounter:int=0;//这会增加快照之间的延迟
public var delayMax:int=7;//更改此数字以加快或减慢拍摄速度
变量:数组=[];
公共函数Main():void
{
玩家=新玩家(舞台,320240);
舞台。儿童(玩家);
//stage.addEventListener(MouseEvent.CLICK,shootBullet,false,0,true);//删除此
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseHandler,false,0,true);
stage.addEventListener(Event.ENTER_FRAME,循环,false,0,true);
对于(变量numBaddies=0;numBaddies 0)
{
对于(变量i:int=bulletList.length-1;i>=0;i--)
{
项目清单[i].loop();
}
}

对于(var h=0;h您将获得TypeError,因为将
root
强制转换为
MovieClip
失败。当您在括号中用前面的类型名称括住变量时,您是在断言对象属于该类型(或扩展该类型)。在Bullet.removeSelf()函数中,您有:
MovieClip(root)
-但在这种情况下,
root
属于
Stage
类型,它不扩展
MovieClip
,从而导致错误*

诚然,
Stage
类型同时具有
contains
removeChild
方法,因此您可以删除强制转换。事实上,Bullet类已经以
stageRef
的形式引用了该Stage,因此您可以重用它。尝试将
removeSelf()
函数更改为:

private function removeSelf():void
{
    removeEventListener(Event.ENTER_FRAME, loop);
    if (stageRef.contains(this))
        stageRef.removeChild(this);
}

*(更多信息:
根属性的特定类型因上下文而异。有时它实际上是一个
MovieClip
;有时是
位图
;在您的情况下,如错误所示,它是一个
阶段
——但唯一保证的类型是
显示对象
,这是最小的公分母类型e、 以上所有类型都从中继承。以下是有关
root
属性的Adobe文档:)

非常感谢它工作得非常好,很抱歉,我只是回答了一下,因为这个项目,我前一天晚上没有睡觉,所以昨天我真的很累。现在我对
root
也有了更好的了解,非常感谢。我只剩下一件事要做,在impact/collis上把敌人的电影剪辑从舞台上移除并进行阵列在bullet movieclip中,我尝试过使用敌方的
loop()
函数,但即使是trace station也没有向窗口输出任何内容。你能在这里给我一些提示,说明我可能会出什么问题吗?没问题-很高兴它解决了这个问题。没有仔细查看(这实际上是一个单独的问题/帖子),似乎根本没有调用
loop
方法。您可能希望将调用添加到
Main.loop()中
method,您已经在这里迭代了
敌人
数组。添加该数组后,可能会有一些后续问题需要解决,如果您对它们有特定问题,请发布一个新问题。谢谢,我会尝试一下,看看会发生什么。非常感谢您的帮助。