3

我是一名设计师,在代码世界中尝试我的双手。到目前为止,我已经能够弄清楚事情,但我终于碰壁了。我在这里使用 Flash 工作,所以我无法逃脱它可能不是代码,而是设置的可能性。另外,作为新手,答案可能一直摆在我面前,而我只是陷入了困境。

无论如何,我正在开发一个简单的游戏,当角色在其下方时墙壁会消失,然后在他离开后重新出现。我为此寻求帮助,并且能够以单独的类文件的形式获得一些代码。这太棒了,而且很糟糕

- 太棒了,因为我有可以工作的代码,但是...... - 作为编码新手,我从来没有使用过类,现在我已经将它应用到我自己的项目中,它不起作用。故障排除现在变得更加困难。

在努力寻找答案之后,我想它终于到了我必须寻求帮助的地步。

一切都编译得很好,但我立即收到以下输出错误:

"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;
    }
}

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

4

1 回答 1

0

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

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

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

让我知道这是否有帮助...

于 2013-01-23T14:09:45.190 回答