AS3 Error 1009 when Debugging - actionscript-3

I'm trying to create a game over screen for my space ship game, when player's shields reach 0 it goes to game over screen and stop the gameplay. The game over screen is working, but I can't stop the gameplay. I tried to set the Ship to null when player's shields reach 0 but I got error 1009. And all the gameplay objects (Ship, Enemy...) will loaded to the stage when "public function fGameStart(evt: Event): void {" executes, is there a way that I can stop this function from running when game over? Any help is greatly appreciated!
public class Engine extends MovieClip {
private var preloader: ThePreloader;
public function Engine() {
stage.addEventListener("gameSTART", fGameStart);
stage.addEventListener("gameOVER", fGameOver);
}
private var numStars: int = 80;
public static var enemyList: Array = new Array();
private var ourShip: Ship;
public function fGameStart(evt: Event): void {
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
ourShip.addEventListener("hit", shipHit, false, 0, true);
stage.addChild(ourShip);
for (var i: int = 0; i < numStars; i++) {
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
function loop(e: Event): void {
if (Math.floor(Math.random() * 20) == 5) {
var enemy: Stinger = new Stinger(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
else if (Math.floor(Math.random() * 80) == 5) {
var enemy2: Stinger2 = new Stinger2(stage, ourShip);
enemy2.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy2.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy2);
stage.addChild(enemy2);
}
}
}
public function fGameOver(e: Event) {
gotoAndStop(4);
ourShip = null;
}
}

There is no point setting the ourShip variable to null. It will not remove the DisplayObject from the stage, or remove it from memory. In fact the very reason you get this error is you setting it to null.
What you need to do is stop the loop function from triggering.
public function fGameOver(e: Event) {
gotoAndStop(4);
//ourShip = null;
stage.removeChild(ourShip);
removeEventListener(Event.ENTER_FRAME, loop);
}
also setting a weak reference for you listener might be a bad idea here
//addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
//why not:
addEventListener(Event.ENTER_FRAME, loop);

Related

stopImmediatePropagation doesn't work for SimpleButton

I'm trying to cancel the event in this actionscript3 code:
public class Main extends Sprite
{
public function Main()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
protected function onAddedToStage(event:Event):void
{
this.addEventListener(MouseEvent.MOUSE_OVER, onOver, true, int.MAX_VALUE);
var btn:SimpleButton = new SimpleButton(
createBtnState(0x0000FF),
createBtnState(0x00FFFF),
createBtnState(0x00FF00),
createBtnState(0x0000FF));
btn.x = stage.stageWidth - btn.width >> 1;
btn.y = stage.stageHeight - btn.height >> 1;
addChild(btn);
}
private function createBtnState(color:uint):Sprite
{
var s:Sprite = new Sprite();
s.graphics.beginFill(color, 1);
s.graphics.drawRect(0,0,100,20);
s.graphics.endFill();
return s;
}
protected function onOver(event:MouseEvent):void
{
event.stopImmediatePropagation(); //Don't work
}
}
how to cancel the event hover the button?
In this example, the button responds when you hover.
your hover event seem to be added on the Main Not the Button.
Try putting the event listener on the btn you declared.
protected function onAddedToStage(event:Event):void
{
//this.addEventListener(MouseEvent.MOUSE_OVER, onOver, true, int.MAX_VALUE);
var btn:SimpleButton = new SimpleButton(
createBtnState(0x0000FF),
createBtnState(0x00FFFF),
createBtnState(0x00FF00),
createBtnState(0x0000FF));
btn.x = stage.stageWidth - btn.width >> 1;
btn.y = stage.stageHeight - btn.height >> 1;
addChild(btn);
btn.addEventListener(MouseEvent.MOUSE_OVER, onOver, true, int.MAX_VALUE);
}

AS3 How to call a button from another class?

I'm trying to create a button that when you click on it, the ship fires a laser, but the button isn't working. I mean I didn't get any error when debugging, however it won't allow me to click on the button, but instead it allows me to click on my ship to fire. Any help is greatly appreciated, thanks!
My Fire.as
package control {
import flash.events.Event;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.events.Event;
import objects.Ship;
public class Fire extends MovieClip {
private var my_x: Number;
private var my_y: Number;
private var ourShip: Ship;
var mouseDown: Boolean;
public function Fire(margin_left: Number, margin_bottom: Number, ourShip_mc: Ship) {
my_x = margin_left;
my_y = margin_bottom;
ourShip = ourShip_mc;
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e: Event = null): void {
if (hasEventListener(Event.ADDED_TO_STAGE)) {
removeEventListener(Event.ADDED_TO_STAGE, init);
}
this.x = my_x + this.width / 2;
this.y = stage.stageHeight - my_y - this.height / 2;
this.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(event: MouseEvent): void {
//EVENT DISPATCHER
dispatchEvent(new Event("eventshoot", true));
trace("Fire clicked");
}
}
}
My Ship.as
package objects {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.display.JointStyle;
import control.Controller;
import control.Joystick;
import control.Fire;
public class Ship extends MovieClip {
var mouseDown: Boolean;
private var stageRef: Stage;
private var key: Controller;
private var speed: Number = 2.5;
private var vx: Number = 0;
private var vy: Number = 0;
private var friction: Number = 0.93;
private var maxspeed: Number = 8;
//fire related variables
private var fireTimer: Timer; //causes delay between fires
private var canFire: Boolean = true; //can you fire a laser
public var move_left: Boolean = false;
public var move_up: Boolean = false;
public var move_right: Boolean = false;
public var move_down: Boolean = false;
public function Ship(stageRef: Stage): void {
this.stageRef = stageRef;
key = new Controller(stageRef);
this.addEventListener(Event.ENTER_FRAME, ShipMove);
stage.addEventListener("eventshoot", firenow);
//setup your fireTimer and attach a listener to it.
fireTimer = new Timer(250, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function ShipMove(event: Event): void {
if (move_left)
vx -= speed;
else if (move_right)
vx += speed;
else
vx *= friction;
if (move_up)
vy -= speed;
else if (move_down)
vy += speed;
else
vy *= friction;
}
public function firenow(event: Event) {
fireLaser();
}
public function loop(e: Event): void {
//update position
x += vx;
y += vy;
//speed adjustment
if (vx > maxspeed)
vx = maxspeed;
else if (vx < -maxspeed)
vx = -maxspeed;
if (vy > maxspeed)
vy = maxspeed;
else if (vy < -maxspeed)
vy = -maxspeed;
//ship appearance
rotation = vx;
scaleX = (maxspeed - Math.abs(vx)) / (maxspeed * 4) + 0.75;
//stay inside screen
if (x > stageRef.stageWidth - 30) {
x = stageRef.stageWidth - 30;
vx = -vx;
} else if (x < 30) {
x = 30;
vx = -vx;
}
if (y > stageRef.stageHeight) {
y = stageRef.stageHeight;
vy = -vy;
} else if (y < 0) {
y = 0;
vy = -vy;
}
}
private function fireLaser(): void {
//if canFire is true, fire a laser
//set canFire to false and start our timer
//else do nothing.
if (canFire) {
stageRef.addChild(new LaserGreen(stageRef, x + vx, y - 10));
canFire = false;
fireTimer.start();
}
}
//HANDLERS
private function fireTimerHandler(e: TimerEvent): void {
//Timer ran, fire again.
canFire = true;
}
public function takeHit(): void {
dispatchEvent(new Event("hit"));
}
}
}
Updated, here's the Engine.as, sorry for not replying it to your comment below, the structure is messed up if I do so.
package objects {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import control.Joystick;
import control.Fire;
public class Engine extends MovieClip {
private var preloader: ThePreloader;
public function Engine() {
preloader = new ThePreloader(474, this.loaderInfo);
stage.addChild(preloader);
preloader.addEventListener("loadComplete", loadAssets);
preloader.addEventListener("preloaderFinished", showSponsors);
stage.addEventListener("gameSTART", fGameStart);
}
private function loadAssets(e: Event): void {
this.play();
}
private function showSponsors(e: Event): void {
stage.removeChild(preloader);
var ps: PrerollSponsors = new PrerollSponsors(stage);
ps.addEventListener("prerollComplete", showMenu);
ps.preroll();
}
private function showMenu(e: Event): void {
new MainMenu(stage).load();
}
public static var enemyList: Array = new Array();
private var ourShip: Ship;
private var joystick: Joystick;
private var fire: Fire;
private var scoreHUD: ScoreHUD;
public function fGameStart(evt: Event): void {
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
ourShip.addEventListener("hit", shipHit, false, 0, true);
stage.addChild(ourShip);
joystick = new Joystick(120, 70, ourShip);
addChild(joystick);
fire = new Fire(420, 70, ourShip);
addChild(fire);
scoreHUD = new ScoreHUD(stage);
stage.addChild(scoreHUD);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e: Event): void {
if (Math.floor(Math.random() * 20) == 5) {
var enemy: E1 = new E1(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
} else if (Math.floor(Math.random() * 80) == 5) {
var enemy2: E2 = new E2(stage, ourShip);
enemy2.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy2.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy2);
stage.addChild(enemy2);
}
}
private function enemyKilled(e: Event) {
scoreHUD.updateKills(1);
scoreHUD.updateScore(e.currentTarget.points);
}
private function removeEnemy(e: Event) {
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
private function shipHit(e: Event) {
scoreHUD.updateHits(1);
}
}
}
So every time the fire button (Fire.as) is clicked, it dispatched an event "eventshoot", and the ship (Ship.as) pick it up. And when the ship receive it, the ship itself fires a laser, that's the idea. But since there are prerolls, menus...stuff like that will loaded before starting the game, I can't just simply drag the fire button to the stage. The engine will load the ship, fire button, enemy, score... to the stage when game started. And I got a error 1009 when debugging "TypeError: Error #1009: Cannot access a property or method of a null object reference.", it is from:
stage.addEventListener("eventshoot", fire now);
in Ship.as
I understand that I'm getting this error because there is no fire button on stage, so my ship can't pickup the "eventshoot" event, is there a way I can make the ship only pickup that event after making sure the button is loaded to the stage to avoid the error?
While you certainly can create a class for your button, the functionality to make the ship object fire a laser should not be in the button.
Given a Ship class that looks like this:
package
{
import flash.display.MovieClip;
public class Ship extends MovieClip
{
public function fireLaser():void
{
trace("pew pew");
}
}
}
You can instantiate this class and add it to your main timeline with this code:
var ship:Ship = new Ship();
addChild(ship);
If you placed the symbol by hand you do not need to do this and instead only need to give it an instance name of ship.
To make something clickable, add an event listener to it. For example, to make the ship itself clickable:
var ship:Ship = new Ship();
addChild(ship);
ship.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
function onMouseDown(event:MouseEvent):void
{
trace("ship clicked");
}
If you have a button on the main time line with an instance name of fire, you can as easily add the listener to that button:
var ship:Ship = new Ship();
addChild(ship);
// v---this changed
fire.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
function onMouseDown(event:MouseEvent):void
{
trace("fire button clicked");
}
Last but not least, if you want to call a method on an object instead of using trace(), you can do that, too:
var ship:Ship = new Ship();
addChild(ship);
fire.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
function onMouseDown(event:MouseEvent):void
{
ship.fireLaser(); // this changed, laser fired "pew pew"
}
tl, dr;
The button itself shouldn't do anything. If the button did anything with the ship directly, it would have to know the ship. The button shouldn't know the ship. All the button does is say "I got clicked" by dispatching an event, everything else should be handled outside.
You know, just like when you wrote your question here, it's not the entire internet (including me) sitting in the keyboard buttons of your computer listening to your input. All your keyboard buttons did was saying "I got clicked". Everything else got handled outside, by your operating system, browser, etc.

ActionScript 3 Syntax Errors

I'm new to AS3 and this site. I'm using a tutorial from http://asgamer.com/2009/as3-flash-games-for-beginners-scores-huds-and-user-interface to create my own version of shooting game. The tutorial comes with 1 enemy and 1 game level and I'm trying to add more enemies and levels.
Here's the original Engine.as coding from the tutorial:
package com.asgamer.basics1
{ import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Engine extends MovieClip
{
private var numStars:int = 80;
public static var enemyList:Array = new Array();
private var ourShip:Ship;
private var scoreHUD:ScoreHUD;
public function Engine() : void
{
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
ourShip.addEventListener("hit", shipHit, false, 0, true);
stage.addChild(ourShip);
scoreHUD = new ScoreHUD(stage);
stage.addChild(scoreHUD);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 90) == 5)
{
var enemy:Stinger = new Stinger(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
private function enemyKilled(e:Event)
{
scoreHUD.updateKills(1);
scoreHUD.updateScore(e.currentTarget.points);
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
private function shipHit(e:Event)
{
scoreHUD.updateHits(1);
}
}
}
For enemies, I've created another enemy named Stinger2 and added it under the first enemy loop as below but I got error 1021 and 5000.
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 90) == 5)
{
var enemy:Stinger = new Stinger(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 90) == 5)
{
var enemy:Stinger2 = new Stinger2(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
For levels, I want to create 3 different levels. The first level comes with Stinger only, the second comes with Stinger2 only and the final level comes with both Stinger and Stinger2. There is also a score system from the tutorial, the below coding is from the tutorial, it controls the chance of enemy spawns:
if (Math.floor(Math.random() * 30) == 5
I tried to change it to:
if (Math.floor(Math.random() * 30) == 5 && scoreHUD(value:Number) < 10000)
so Stinger only spawns when player's score is below 10000 (level 1) but then I got a error 1084. So how do I add multiple enemies to the array and how to make these enemies spawn between specific scores?
Error 1021 means you have two functions with the same name (loop), this is not allowed for obvious reasons.
Error 5000 is sometimes ambiguous, it usually happens when you have other problems in your code.
Error 5000: The class 'myClass' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type
You should take a look at your library in flash to make sure you have the correct base classes declared.
Error 1084 is just a simple syntax error, you are missing a ) in your if statement.

As3 hittesting doesn't really work (nested)

I got a tank.
I got a hero (controllable character).
The tank has a nested movieclip which has a very thin surface area.
Yet, the thing detects a collision when it's not even touching the hero.
public class tank_sight extends MovieClip
{
private var _root:MovieClip;
public function tank_sight()
{
addEventListener(Event.ADDED, beginClass);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event:Event):void
{
if(this.hitTestObject(_root.hero.hitbox))
{
this.gotoAndStop(2);
trace("HIT");
fire();
}
else
{
this.gotoAndStop(1);
}
}
private function fire():void
{
var shell:Shell = new Shell(x, y, rotation - 180);
_root.addChild(shell);
}
}
What's wrong? I don't get it.
EDIT: Sight is rotating, so that's probably why. I tried using this code on the player class:
point = _root.tanks.barrel.sight.localToGlobal(new Point());
if(this.hitTestPoint(point.x, point.y, false))
{
trace("HIT");
}
But it doesn't work.. It never traces "HIT", unless I stand in some weird location at certain times.
hitTestObject works with nested objects also (display objects that have different parents), you should check logic of your game, and do some tests.
Simple example:
var squareHolder:Sprite = new Sprite();
var squareInner:Shape = new Shape();
var hitHolder:Sprite = new Sprite();
var hitCircle:Shape = new Shape();
hitCircle.graphics.beginFill(0x990000);
hitCircle.graphics.drawCircle(0, 0, 20);
squareInner.graphics.beginFill(0x009900);
squareInner.graphics.drawRect(0, 0, 40, 40);
addChild(squareHolder);
squareHolder.addChild(squareInner);
squareHolder.x = 200;
squareHolder.y = 100;
squareInner.x = 50;
squareInner.y = 50;
stage.addChild(hitHolder);
hitHolder.addChild(hitCircle);
hitCircle.transform.matrix = new Matrix(1, 0.5, 0.5, 1, 30, 30);
stage.addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void {
hitHolder.x = e.stageX;
hitHolder.y = e.stageY;
if (hitCircle.hitTestObject(squareInner)) {
trace("Ding!");
}
});
Whatever you do with hitCircle (visible=false, trasparent fill, transformations) it will still work.

Reaching variables in other functions

Im making a game as a graduation project and have encountered a little issue. The goal of the game is to maneuver a ship and avoid asteroids as long as possible. I'm almost done but I'm now trying to solve the collision detection.
My problem is how do I reach the enemy in the loop variable so that it can check for collision with it? Here is the code:
package com.asgamer.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Engine extends MovieClip
{
private var numStars:int = 80;
private static var enemyList:Array = new Array();
private var ourShip:Ship;
public function Engine() : void
{
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
stage.addChild(ourShip);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1)
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, krash);
}
private function krash(e:Event) : void
{
function krash(e:Event):void
{
if (enemy.hitTestObject(ourShip)==true)
{
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash);
}
}
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 10) == 5)
{
var enemy:Asteroid = new Asteroid(stage);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
}
}
As you can see I have a function for making enemies which is called loop, and a function for collision detection called krash. But since enemy is a variable inside loop, how can I check for collision with it in the krash function. (Sort of new to actionscript, so I dont know the terminology all to well)
In krash you will need to check each enemy in enemyList.
example:
private function krash(e:Event) : void
{
function krash(e:Event):void
{
for(var i:int = 0; i < enemyList.length; i++)
{
if (enemyList[i].hitTestObject(ourShip)==true)
{
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash);
}
}
}
}
Unfortunately, if you have a lot of enemies on the stage the program will lag a bit because you're checking for collisions with all of them every frame.
Also, I'd reccommend you add something to remove you enemies from the screen :P
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
stage.removeChild(enemyList.indexOf(e.currentTarget));
}
Hope this helps.