Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that </p> <pre><code>var canon:MovieClip; </code></pre> <p>is defined as a private variable and is available inside all class methods, where as </p> <pre><code>var canon:Loader = new Loader(); </code></pre> <p>you define in init function is available only in this function scope. After function ends, all variables defined in functions scope are discarded. Also note that first and second variable are two different variables.</p> <p>To fix your problem you should make class variable to a Loader and during init function create loader and assign the value to it. This way it will be available in all functions in this class, like such</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: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 &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>
 

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