简体   繁体   English

加载图像时遇到问题,而不是在actionscript-3中使用绘制的对象

[英]Having trouble loading an image instead of using a drawn object in actionscript-3

I am trying to make a ball shoot from a canon, which is fine, but i want to change the canon from a drawn canon into an image i have OF a canon. 我正试图从一个佳能制作一个球,这很好,但我想把一个经典从一个绘制的经典变成我有一个经典的图像。 But when i do this i get the following error when i click on the stage to shoot the balls while with the drawn version the balls shoot as planned: 但是,当我这样做时,我得到以下错误,当我点击舞台拍摄球,而绘制版本球按计划拍摄:

TypeError: Error #1009: Cannot access a property or method of a null object reference. TypeError:错误#1009:无法访问空对象引用的属性或方法。 at code2/createNewBullet() at code2/mouseClickHandler() at code2 / createNewBullet()at code2 / mouseClickHandler()

This is the code that works: 这是有效的代码:

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);

This is what i am trying to do to use an image instead of a drawn rectangle for the cannon ((The image loads fine but causes the error)) 这是我试图使用图像而不是用于加农炮的绘制矩形 ((图像加载正常但导致错误))

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);


    }

And this is the full code if needed: 如果需要,这是完整的代码:

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

} }

canon has no scope in your createNewBullet() method. canoncreateNewBullet()方法中没有作用域。

You need to declare canon as a class member variable. 您需要将canon声明为类成员变量。

Currently canon is a local variable to the init() method. 目前canon是init()方法的局部变量。

As pointed out by the commenter, you do need to change the canon to a Loader type in your declarations. 正如评论者所指出的,您需要在声明中将canon更改为Loader类型。

The problem is that 问题是

var canon:MovieClip;

is defined as a private variable and is available inside all class methods, where as 被定义为私有变量,并且在所有类方法中都可用,其中as

var canon:Loader = new Loader();

you define in init function is available only in this function scope. 你在init函数中定义只在这个函数范围内可用。 After function ends, all variables defined in functions scope are discarded. 函数结束后,将丢弃函数作用域中定义的所有变量。 Also note that first and second variable are two different variables. 另请注意,第一个和第二个变量是两个不同的变量。

To fix your problem you should make class variable to a Loader and during init function create loader and assign the value to it. 要解决您的问题,您应该将类​​变量设置为Loader,并在init函数期间创建加载程序并为其赋值。 This way it will be available in all functions in this class, like such 这样它就可以在这个类的所有函数中使用

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM