actionscript3 ArgumentError:错误#2015:无效的BitmapData

时间:2012-11-25 23:43:34

标签: flash class actionscript bitmap

我是一名设计师,在代码世界中尝试着我的双手。到目前为止,我已经能够解决问题,但我终于碰到了一堵墙。我在这里工作,所以可能性并没有逃避我可能不是代码,而是设置。此外,作为新的,有可能这个答案一直在我面前,我只是走了一个兔子洞。

无论如何,我正在做一个简单的游戏,当一个角色在他们下面时需要墙壁消失,然后一旦他离开就重新出现。我求助于此,并能够以单独的类文件的形式获得一些代码。

这是非常糟糕的

- 太棒了,因为我的代码有效,但...... - 编写新东西,我从未使用过类,现在我已经将它应用到我自己的项目中,它不起作用。麻烦之后现在变得更加困难。

在努力寻找答案后,我认为最终我必须寻求帮助。

所有内容编译得很好,但我立即遇到以下输出错误:

"Attempting to launch and connect to Player using URL ""\flashDemoCS5.5-47.swf
[SWF] ""\flashDemoCS5.5-47.swf - 8512188 bytes after decompression
ArgumentError: Error #2015: Invalid BitmapData.
    at flash.display::BitmapData/ctor()
    at flash.display::BitmapData()
    at Wall()[""\Wall.as:15]
    at wall_003()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at Wall()[""\Wall.as:13]
    at wall()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at flashDemoCS5_fla::MainTimeline()
Cannot display source code at this location.
"

给我的代码在演示中工作,演示本身就是附加的。演示功能的.fla无论如何都没有编码。我先发布一下。它是Wall.as,它与主.fla文件位于同一个文件夹中:

    package  {

    import flash.display.MovieClip;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.geom.Point;


    public class Wall extends MovieClip {

        var bmp:BitmapData;

        public function Wall() {
            this.addEventListener(Event.ENTER_FRAME, hideWall, false, 0, false);
            bmp = new BitmapData (this.width, this.height, true, 0);
            bmp.draw(this);
        }

        public function hideWall(e:Event){
            if(bmp.hitTest(new Point(0, 0), 0, this.globalToLocal(new Point(stage.mouseX, stage.mouseY)))) {
                this.visible = false;
            }else{
                this.visible = true;
            }
        }

    }

}

附加到主.fla文件的代码位于第一帧的自己的层中:

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);


stage.scaleMode = StageScaleMode.NO_SCALE; // or other scale modes...
stage.align = StageAlign.TOP_LEFT;

stage.addEventListener(Event.RESIZE, resizeHandler);

function resizeHandler(e:Event):void{
            trace(stage.stageWidth + "x" + stage.stageHeight);
}

;
var animationState:String = "idle";
var mirrorState:String = "walk";
var keyCollected:Boolean = false;
var doorOpen:Boolean = false;

//collisions
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;

//player collision points (relative to anchor point)
var leftBumpPoint:Point = new Point(-50,100);
var rightBumpPoint:Point = new Point(50,100);
var upBumpPoint:Point = new Point(0,75);
var downBumpPoint:Point = new Point(0,150);

//button press
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;

//scrollspeed
var xSpeed:int = 0;
var ySpeed:int = 0;

var speedConstant:int = 5;

var friction:Number = 0.80;

//identify player location
var scrollX:int = 0;
var scrollY:int = 0;

//identify current level
var currentLevel:int = 1;

//next level function
function nextLevel():void{
     currentLevel++;
     //trace("Next Level: " + currentLevel);
     if(currentLevel == 2){
          gotoLevel2();
     }
     if(currentLevel == 3){
          gotoLevel1();
     }
}

//level 3 is the same as level 1.  3 will go back to 1 in the code so level 2 can be accessed agin.
function gotoLevel1():void{
    back.other.gotoAndStop(1);
    back.visuals.gotoAndStop(1);
    back.collisions.gotoAndStop(1);
    scrollX = 0;
    scrollY = 0;
    keyCollected = false;
    back.other.doorKey.visible = true;
    doorOpen = false;
    back.other.lockedDoor.gotoAndStop(1);
    currentLevel = 1;
}

function gotoLevel2():void{
     back.other.gotoAndStop(2); //updates door and key
     back.visuals.gotoAndStop(2); //updates the visuals
     back.collisions.gotoAndStop(2); //updates the collisions
     scrollX = 0; //resets the player's x position in the new level
     scrollY = 500; //resets the player's y position in the new level
     keyCollected = false; //resets the keyCollected variable
     back.other.doorKey.visible = true; //makes the key visible again
     doorOpen = false; //resets the doorOpen variable
     back.other.lockedDoor.gotoAndStop(1); //makes the door return to its locked image
}

function loop(e:Event):void
{


    if (back.collisions.hitTestPoint(player.x + leftBumpPoint.x,player.y + leftBumpPoint.y,true))
    {
        //trace("leftBumping");
        leftBumping = true;
    }
    else
    {
        leftBumping = false;
    }

    if (back.collisions.hitTestPoint(player.x + rightBumpPoint.x,player.y + rightBumpPoint.y,true))
    {
        //trace("rightBumping");
        rightBumping = true;
    }
    else
    {
        rightBumping = false;
    }

    if (back.collisions.hitTestPoint(player.x + upBumpPoint.x,player.y + upBumpPoint.y,true))
    {
        //trace("upBumping");
        upBumping = true;
    }
    else
    {
        upBumping = false;
    }

    if (back.collisions.hitTestPoint(player.x + downBumpPoint.x,player.y + downBumpPoint.y,true))
    {
        //trace("downBumping");
        downBumping = true;
    }
    else
    {
        downBumping = false;
    }

    if (leftPressed)
    {
        xSpeed -=  speedConstant;

    }
    else if (rightPressed)
    {
        xSpeed +=  speedConstant;

    }

    if (upPressed)
    {
        ySpeed -=  speedConstant;

    }
    else if (downPressed)
    {
        ySpeed +=  speedConstant;

    }

    //colision react
    if (leftBumping)
    {
        if (xSpeed < 0)
        {
            xSpeed *=  -0.5;
        }
    }

    if (rightBumping)
    {
        if (xSpeed > 0)
        {
            xSpeed *=  -0.5;
        }
    }

    if (upBumping)
    {
        if (ySpeed < 0)
        {
            ySpeed *=  -0.5;
        }
    }

    if (downBumping)
    {
        if (ySpeed > 0)
        {
            ySpeed *=  -0.5;
        }
    }
    if (ySpeed && xSpeed || xSpeed || ySpeed> 10){
        (ySpeed + xSpeed)/2;
        trace(ySpeed + xSpeed);
        trace(ySpeed);
        trace(xSpeed);
    }
    else if(ySpeed && xSpeed || xSpeed || ySpeed < -10){
        (ySpeed + xSpeed)/2;
        trace(ySpeed + xSpeed);
        trace(ySpeed);
        trace(xSpeed);
    }
    //smoothes scrolling
    xSpeed *=  friction;
    ySpeed *=  friction;

    scrollX -=  xSpeed;
    scrollY -=  ySpeed;

    back.x = scrollX;
    back.y = scrollY;

        //key controls
    if(keyCollected == false){ // if we still haven't collected the key
if(player.hitTestObject(back.other.doorKey)){ // and if the player collides with the key
back.other.doorKey.visible = false; // hide the key from view
keyCollected = true; // set our Boolean to true
}
}
    if(doorOpen == false){ // if the door hasn't been opened yet
     if(keyCollected == true){ // and if the player has already collected the key
          if(player.hitTestObject(back.other.lockedDoor)){ // check if the door and the player are touching
               // if all of these conditions are met...
               back.other.lockedDoor.gotoAndStop(2); // ...switch the door's image to its 2nd frame
               doorOpen = true; // ...set the variable to true//level change.  Move this later.
if(doorOpen && player.hitTestObject(back.other.lockedDoor)){
          //proceed to the next level if the player is touching an open door
          nextLevel();
     }
          }
     }

    if(doorOpen || player.hitTestObject(back.other.openDoor)){
        nextLevel();
    }
    }


    //animation
    if (leftPressed || rightPressed || downPressed || xSpeed > speedConstant || xSpeed < ( speedConstant *-1 ) ){
        animationState = "walk";
    }else if(upPressed || upPressed && rightPressed || upPressed && leftPressed){
        animationState = "walk_up";
    }else{ 
        player.prevFrame();
    }


    //makse player face direction he/she is going;
    if (leftPressed && !rightPressed)
    {
        player.scaleX = -.7;
    }
    else if (rightPressed && !leftPressed)
    {
        player.scaleX = .7;
    }
    //stop animation
    if (player.currentLabel != animationState)
    {
        player.gotoAndStop(animationState);
    }

}






function keyDownHandler(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.LEFT)
    {
        leftPressed = true;

    }
    else if (e.keyCode == Keyboard.RIGHT)
    {
        rightPressed = true;

    }
    else if (e.keyCode == Keyboard.UP)
    {
        upPressed = true;

    }

    else if (e.keyCode == Keyboard.DOWN)
    {
        downPressed = true;
    }
}

function keyUpHandler(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.LEFT)
    {
        leftPressed = false;

    }
    else if (e.keyCode == Keyboard.RIGHT)
    {
        rightPressed = false;

    }
    else if (e.keyCode == Keyboard.UP)
    {
        upPressed = false;

    }
    else if (e.keyCode == Keyboard.DOWN)
    {
        downPressed = false;
    }
}

希望我已经提供了所需的一切。

1 个答案:

答案 0 :(得分:0)

创建新的MovieClip时,它没有内容,其尺寸为零。当Wall扩展MovieClip时,这就是你正在做的事情(扩展一个类意味着你将它用作模板来添加额外的功能,非常简单) - 然后你根据父MovieClip的维度创建一个位图( 0x0)并尝试将其绘制到它。这就是破坏你的代码的原因。

我假设您使用的是Flash,而不是Flex / Flash Builder,如果有,您是否有想要用于墙的图形?如果将其转换为MovieClip,请右键单击并选择库中的“属性”,然后选择“为ActionScript导出”,将为其创建自定义类。当您创建此对象的新实例时,它包含图形数据。

如果您想要添加自定义功能,请创建一个扩展此而不是MovieClip 的类,但请确保在构造函数中调用 super()(该函数)与班级同名。)

如果这有帮助,请告诉我......