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