Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble loading an image instead of using a drawn object in actionscript-3
    primarykey
    data
    text
    <p>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:</p> <blockquote> <p>TypeError: Error #1009: Cannot access a property or method of a null object reference. at code2/createNewBullet() at code2/mouseClickHandler()</p> </blockquote> <p><strong>This is the code that works:</strong></p> <pre><code>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); </code></pre> <p><strong>This is what i am trying to do to use an image instead of a drawn rectangle for the cannon</strong> ((The image loads fine but causes the error))</p> <pre><code>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); } </code></pre> <p><strong>And this is the full code if needed:</strong></p> <pre><code>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 &lt; 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 &gt;= gameHeight) { removeChild(bullets[i]); bullets.splice(i, 1); } } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload