0

我正在尝试从佳能拍摄球,这很好,但我想将佳能从绘制的佳能更改为我拥有的佳能图像。但是当我这样做时,当我点击舞台射击球时,我得到以下错误,而绘制版本的球按计划射击:

TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 code2/createNewBullet() 在 code2/mouseClickHandler()

这是有效的代码:

private function init():void {

        drawBoard = new MovieClip;
        drawBoard.graphics.beginFill(0xFFFFFF, 0); // white transparant
        drawBoard.graphics.drawRect(0, 0, gameWidth, gameHeight);
        drawBoard.graphics.endFill();
        addChild(drawBoard);
        // create canon
        canon = new MovieClip;
        canon = new MovieClip;
        canon.graphics.beginFill(0x000000);
        canon.graphics.drawRect(0, -10, 50, 20);
        canon.graphics.endFill();
        canon.rotation = -45;
        canon.x = 25;
        canon.y = gameHeight;
        addChild(canon);

这就是我试图使用图像而不是为大炮绘制的矩形 ((图像加载正常但会导致错误))

private function init():void {
        // create drawboard (the important movieclip, that holds all visible elements)
        drawBoard = new MovieClip;
        drawBoard.graphics.beginFill(0xFFFFFF, 0); // white transparant
        drawBoard.graphics.drawRect(0, 0, gameWidth, gameHeight);
        drawBoard.graphics.endFill();
        addChild(drawBoard);

        var canon:Loader = new Loader();
        var fileRequest:URLRequest = new URLRequest("cannon1.png");
        canon.load(fileRequest);
        canon.rotation = -45;
        canon.x = 25;
        canon.y = gameHeight;
        addChild(canon);


    }

如果需要,这是完整的代码:

package {

// Copyright 2010-2011 - Seinia.com
// Find more crazy good AS3.0 tutorials and games on Seinia.com!

// imports
// --------------------------------------------------------------------------------------
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.events.*;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;

// Game class (MovieClip extension)
// --------------------------------------------------------------------------------------
public class code2 extends MovieClip {

    // game properties
    // --------------------------------------------------------------------------------------
    var drawBoard:MovieClip;
    var background:MovieClip;
    var gameWidth:int = 600;
    var gameHeight:int = 300;
    var gravity:Number = .4;
    var bullets:Array;
    var canon:MovieClip;

    // constructor
    // --------------------------------------------------------------------------------------
    public function code2():void {
        this.focusRect = false;
        this.init();
        addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
        addEventListener(MouseEvent.CLICK, mouseClickHandler, false, 0, true);
        bullets = new Array();
    }

    // init the game
    // --------------------------------------------------------------------------------------
    private function init():void {
        // create drawboard (the important movieclip, that holds all visible elements)
        drawBoard = new MovieClip;
        drawBoard.graphics.beginFill(0xFFFFFF, 0); // white transparant
        drawBoard.graphics.drawRect(0, 0, gameWidth, gameHeight);
        drawBoard.graphics.endFill();
        addChild(drawBoard);

        var canon:Loader = new Loader();
        var fileRequest:URLRequest = new URLRequest("cannon1.png");
        canon.load(fileRequest);
        canon.rotation = -45;
        canon.x = 25;
        canon.y = gameHeight;
        addChild(canon);


    }

    // mouse click handler
    // --------------------------------------------------------------------------------------
    private function mouseClickHandler(event:MouseEvent):void {
        createNewBullet();
    }

    // function to create a new bullet
    // --------------------------------------------------------------------------------------
    private function createNewBullet():void {
        // init and draw bullet
        var bullet:MovieClip = new MovieClip;
        bullet.graphics.beginFill(0xff0000);
        bullet.graphics.drawCircle(0, 0, 10);
        bullet.graphics.endFill();
        // define bullet start point and speed
        var cos:Number = Math.cos(canon.rotation * Math.PI / 180);
        var sin:Number = Math.sin(canon.rotation * Math.PI / 180);
        var speed:Number = 8;
        bullet.x = canon.x + cos * canon.width;
        bullet.y = canon.y + sin * canon.width;
        bullet.vx = cos * speed;
        bullet.vy = sin * speed;
        bullets.push(bullet);
        addChild(bullet);
    }

    // enter frame handler
    // --------------------------------------------------------------------------------------
    private function enterFrameHandler(event:Event):void {
        for (var i = 0; i < bullets.length; i++ ) {
            // gravity
            bullets[i].vy += gravity;
            // move bullet
            bullets[i].x += bullets[i].vx;
            bullets[i].y += bullets[i].vy;
            // remove star from stage?
            if (bullets[i].y >= gameHeight) {
                removeChild(bullets[i]);
                bullets.splice(i, 1);
            }
        }
    }
}

}

4

2 回答 2

1

canoncreateNewBullet()在您的方法中没有范围。

您需要声明canon为类成员变量。

目前 canon 是该init()方法的局部变量。

正如评论者所指出的,您确实需要在声明中将规范更改为 Loader 类型。

于 2013-04-17T19:40:18.523 回答
1

问题是

var canon:MovieClip;

被定义为私有变量,并且在所有类方法中都可用,其中

var canon:Loader = new Loader();

您在 init 函数中定义的函数仅在此函数范围内可用。函数结束后,函数范围内定义的所有变量都将被丢弃。另请注意,第一个和第二个变量是两个不同的变量。

要解决您的问题,您应该将类​​变量设置为 Loader,并在 init 函数期间创建 loader 并将值分配给它。这样它就可以在这个类的所有函数中使用,就像这样

package {

// Copyright 2010-2011 - Seinia.com
// Find more crazy good AS3.0 tutorials and games on Seinia.com!

// imports
// --------------------------------------------------------------------------------------
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.events.*;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;

// Game class (MovieClip extension)
// ------------------------------------------------------------------------------------    --
public class code2 extends MovieClip {

    // game properties
    // --------------------------------------------------------------------------------    ------
    var drawBoard:MovieClip;
        var background:MovieClip;
    var gameWidth:int = 600;
    var gameHeight:int = 300;
    var gravity:Number = .4;
    var bullets:Array;
    var canon:Loader;

    // constructor
    // --------------------------------------------------------------------------------    ------
    public function code2():void {
        this.focusRect = false;
        this.init();
        addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
        addEventListener(MouseEvent.CLICK, mouseClickHandler, false, 0, true);
        bullets = new Array();
    }

    // init the game
    // --------------------------------------------------------------------------------    ------
    private function init():void {
        // create drawboard (the important movieclip, that holds all visible elements)
        drawBoard = new MovieClip;
        drawBoard.graphics.beginFill(0xFFFFFF, 0); // white transparant
        drawBoard.graphics.drawRect(0, 0, gameWidth, gameHeight);
        drawBoard.graphics.endFill();
        addChild(drawBoard);

        canon = new Loader();
        var fileRequest:URLRequest = new URLRequest("cannon1.png");
        canon.load(fileRequest);
        canon.rotation = -45;
        canon.x = 25;
        canon.y = gameHeight;
        addChild(canon);


    }

    // mouse click handler
    // --------------------------------------------------------------------------------    ------
    private function mouseClickHandler(event:MouseEvent):void {
        createNewBullet();
    }

    // function to create a new bullet
    // --------------------------------------------------------------------------------    ------
    private function createNewBullet():void {
        // init and draw bullet
        var bullet:MovieClip = new MovieClip;
        bullet.graphics.beginFill(0xff0000);
        bullet.graphics.drawCircle(0, 0, 10);
        bullet.graphics.endFill();
        // define bullet start point and speed
        var cos:Number = Math.cos(canon.rotation * Math.PI / 180);
        var sin:Number = Math.sin(canon.rotation * Math.PI / 180);
        var speed:Number = 8;
        bullet.x = canon.x + cos * canon.width;
        bullet.y = canon.y + sin * canon.width;
        bullet.vx = cos * speed;
        bullet.vy = sin * speed;
        bullets.push(bullet);
        addChild(bullet);
    }

    // enter frame handler
    // --------------------------------------------------------------------------------    ------
    private function enterFrameHandler(event:Event):void {
        for (var i = 0; i < bullets.length; i++ ) {
            // gravity
                bullets[i].vy += gravity;
            // move bullet
            bullets[i].x += bullets[i].vx;
            bullets[i].y += bullets[i].vy;
            // remove star from stage?
            if (bullets[i].y >= gameHeight) {
                removeChild(bullets[i]);
                bullets.splice(i, 1);
            }
        }
    }
}

}

于 2013-04-17T19:43:20.690 回答