在actionscript-3中加载图像而不是使用绘制对象时遇到问题
我试着从一部佳能上射出一个球,这很好,但我想把一部佳能从一部画好的佳能变成一部佳能的图像。 但是当我这样做的时候,当我点击舞台射出球时,我会得到以下错误,而使用绘制的版本,球会按计划射出: TypeError:错误#1009:无法访问空对象引用的属性或方法。 代码2/createNewBullet()处 代码为2/mouseClickHandler() 这是有效的代码:在actionscript-3中加载图像而不是使用绘制对象时遇到问题,actionscript-3,flash,flash-cs5,Actionscript 3,Flash,Flash Cs5,我试着从一部佳能上射出一个球,这很好,但我想把一部佳能从一部画好的佳能变成一部佳能的图像。 但是当我这样做的时候,当我点击舞台射出球时,我会得到以下错误,而使用绘制的版本,球会按计划射出: TypeError:错误#1009:无法访问空对象引用的属性或方法。 代码2/createNewBullet()处 代码为2/mouseClickHandler() 这是有效的代码: private function init():void { drawBoard = new MovieCl
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);
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);
}
}
}
}
这就是我试图为大炮使用图像而不是绘制的矩形所做的
((图像加载良好,但会导致错误))
如果需要,这是完整的代码:
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);
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);
}
}
}
}
包{
//版权所有2010-2011-Seinia.com
//在Seinia.com上找到更多疯狂好的AS3.0教程和游戏!
//进口
// --------------------------------------------------------------------------------------
导入flash.display.MovieClip;
导入flash.events.Event;
导入flash.events.MouseEvent;
导入flash.geom.Point;
导入flash.events.*;
导入flash.net.URLRequest;
导入flash.display.Loader;
导入flash.display.Sprite;
//游戏类(MovieClip扩展)
// --------------------------------------------------------------------------------------
公共类代码2扩展了MovieClip{
//游戏属性
// --------------------------------------------------------------------------------------
var抽油板:MovieClip;
背景:MovieClip;
变量gameWidth:int=600;
变量gameHeight:int=300;
变量重力:数字=0.4;
变量:数组;
var佳能:电影唇;
//建造师
// --------------------------------------------------------------------------------------
公共函数代码2():void{
this.focusRect=false;
this.init();
addEventListener(Event.ENTER_FRAME,enterFrameHandler,false,0,true);
addEventListener(MouseEvent.CLICK、mouseClickHandler、false、0、true);
项目符号=新数组();
}
//开始游戏
// --------------------------------------------------------------------------------------
私有函数init():void{
//创建drawboard(包含所有可见元素的重要movieclip)
drawBoard=新的MovieClip;
drawBoard.graphics.beginll(0xFFFFFF,0);//白色透明
drawBoard.graphics.drawRect(0,0,游戏宽度,游戏高度);
drawBoard.graphics.endFill();
addChild(绞车);
变量佳能:加载器=新加载器();
var fileRequest:URLRequest=newurlrequest(“cannon1.png”);
佳能加载(文件请求);
佳能旋转=-45;
佳能x=25;
佳能y=游戏高度;
addChild(佳能);
}
//鼠标点击处理程序
// --------------------------------------------------------------------------------------
专用函数mouseClickHandler(事件:MouseEvent):无效{
createNewBullet();
}
//函数创建新的项目符号
// --------------------------------------------------------------------------------------
私有函数createNewBullet():void{
//初始化并提取子弹
var bullet:MovieClip=新的MovieClip;
bullet.graphics.beginll(0xff0000);
项目符号。图形。画圈(0,0,10);
bullet.graphics.endFill();
//定义项目符号起点和速度
变量cos:Number=Math.cos(canon.rotation*Math.PI/180);
变量sin:Number=Math.sin(canon.rotation*Math.PI/180);
var速度:数字=8;
bullet.x=佳能.x+cos*佳能宽度;
bullet.y=canon.y+sin*canon.width;
bullet.vx=cos*速度;
bullet.vy=sin*速度;
子弹。推(子弹);
addChild(子弹);
}
//输入框架处理程序
// --------------------------------------------------------------------------------------
私有函数enterFrameHandler(事件:事件):void{
对于(变量i=0;i=游戏高度){
removeChild(子弹[i]);
项目符号.拼接(i,1);
}
}
}
}
}
canon
在您的createNewBullet()
方法中没有作用域
您需要将canon
声明为类成员变量
目前,canon是init()
方法的局部变量
正如评论员所指出的,您确实需要在声明中将佳能更改为加载器类型。问题在于
var canon:MovieClip;
定义为私有变量,在所有类方法中都可用,其中
var canon:Loader = new Loader();
您在init中定义的函数仅在此函数范围内可用。函数结束后,将丢弃函数范围中定义的所有变量。
还要注意,第一个和第二个变量是两个不同的变量
要解决您的问题,您应该将类变量设置为加载器
在初始化函数期间,创建加载程序并为其赋值。这样,它将在这个类中的所有函数中都可用,例如
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);
}
}
}
}
包{
//版权所有2010-2011-Seinia.com
//在Seinia.com上找到更多疯狂好的AS3.0教程和游戏!
//进口
// --------------------------------------------------------------------------------------
导入flash.display.MovieClip;
导入flash.events.Event;
导入flash.events.MouseEvent;
导入flash.geom.Point;
导入flash.events.*;
导入flash.net.URLRequest;
导入flash.display.Loader;
导入flash.display.Sprite;
//游戏类(MovieClip扩展)
// ------------------------------------------------------------------------------------ -