objects follow the same class - arrays

I have a problem, I am working on a larger project, and I made a small project to test this problem.
The problem being:
I have made a button which spawns enemy's. When an enemy hits allyCopy it bounces back in the opposite direction. But the problem is that EVERY instance of enemy bounces back when a single one hits the test object.
My code:
my main class:
package {
import flash.display.*;
import flash.ui.*;
import flash.events.*;
public class MainClass extends MovieClip {
public var speed:int = 3;
public static var enemyArray:Array = [];
public var randNumber:Number = 0;
public static var allyCopy1 = new allyCopy();
public function MainClass() {
stage.addEventListener(Event.ENTER_FRAME, update);
spawnButton1.addEventListener(MouseEvent.CLICK, spawnEnemy);
allyCopy1.addEventListener(MouseEvent.MOUSE_DOWN, dragOn, true);
allyCopy1.addEventListener(MouseEvent.MOUSE_UP, dragOff, true);
addChild(allyCopy1);
allyCopy1.x = 60,25;
allyCopy1.y = 208,05;
}
public function update(e:Event)
{
/*for (var i:Number = 0; i < enemyArray.length; i++)
{
enemyArray[i].x -= speed;
}*/
}
public function dragOn (e:MouseEvent)
{
allyCopy1.startDrag();
trace("drag");
}
public function dragOff (e:MouseEvent)
{
allyCopy1.stopDrag();
}
public function spawnEnemy(e:MouseEvent)
{
randNumber = Math.random();
trace(randNumber);
var enemy1 = new enemy ();
addChild(enemy1);
enemyArray.push(enemy1);
if (randNumber <= .5)
{
enemy1.x = 526.25;
enemy1.y = 68.05;
} else
{
enemy1.x = 526.25;
enemy1.y = 200.05;
}
}
}
}
The enemy class:
package {
import flash.display.*;
import flash.events.*;
public class enemy extends MovieClip {
public static var hp:int = 100;
public static var speed = 3;
public function enemy() {
addEventListener(MouseEvent.CLICK, killEnemy);
addEventListener(Event.ENTER_FRAME, update);
}
public function update (e:Event) {
x -= speed;
if ( x <= 0)
{
MainClass.enemyArray.pop();
trace(MainClass.enemyArray);
}
if(MainClass.allyCopy1.hitTestObject(this))
{
speed = -3;
}
}
public function killEnemy (e:MouseEvent)
{
this.parent.removeChild(this);
trace(MainClass.enemyArray);
MainClass.enemyArray.pop();
}
}
}

Well the problem is that speed is static variable. This means that it's not separate for each enemy, but it's common (shared) for all instances. So if you change it, it changes everywhere..
Don't do variables static that often - it's pretty bad practice. The very same thing will happen with life - if you decrease the life of one enemy, it will decrease the life of all enemies (all instances of that class).

Related

#1009: Cannot access a property or method of a null object reference. with random dropping squares

i am rebuilding a top down shooter form a .fla files with actions in it to a separate .as file
managed to fix allot of problems with is but i stumbled upon a problem with a function that drops random squares in to a particleContainer and adds them to stage to float down.
when the page is loaded is get allot of
TypeError: Error #1009: Cannot access a property or method of a null object reference. at Level/generateParticles()
i really hope someone can help me fix this! and show me where i made a mistake, i have tried allot of things already but none of them seem to work:(
package {
import playerAuto
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
import flash.text.TextField;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.utils.*;
import flash.display.Shape;
import flash.display.DisplayObject
/**
*
*/
public class Level extends Sprite
{
//these booleans will check which keys are down
public var leftIsPressed:Boolean = false;
public var rightIsPressed:Boolean = false;
public var upIsPressed:Boolean = false;
public var downIsPressed:Boolean = false;
//how fast the character will be able to go
public var speed:Number = 5;
public var vx:Number = 0;
public var vy:Number = 0;
//how much time before allowed to shoot again
public var cTime:int = 0;
//the time it has to reach in order to be allowed to shoot (in frames)
public var cLimit:int = 12;
//whether or not the user is allowed to shoot
public var shootAllow:Boolean = true;
//how much time before another enemy is made
public var enemyTime:int = 0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible :O
public var enemyLimit:int = 16;
//the PlayerAuto's score
public var score:int = 0;
//this movieclip will hold all of the bullets
public var bulletContainer:MovieClip = new MovieClip();
//whether or not the game is over
public var gameOver:Boolean = false;
private var PlayerAuto:playerAuto = new playerAuto;
// publiek toegangkelijke verwijzing naar deze class
public static var instance:Level;
public var particleContainer:MovieClip = new MovieClip();
// constructor code
public function Level()
{
instance = this;
PlayerAuto.x = 200;
PlayerAuto.y = 100;
addChild(this.PlayerAuto);
Project.instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
Project.instance.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
Project.instance.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
Project.instance.stage.addEventListener(Event.ENTER_FRAME, generateParticles);
drieButton.addEventListener( MouseEvent.CLICK, loadScreenThree );
//checking if there already is another particlecontainer there
if(particleContainer == null)
{
//this movieclip will hold all of the particles
addChild(this.particleContainer);
}
}
function keyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.LEFT : leftIsPressed = true; break;
case Keyboard.RIGHT : rightIsPressed = true; break;
case Keyboard.UP : upIsPressed = true; break;
case Keyboard.DOWN : downIsPressed = true; break;
}
}
function keyUpHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.LEFT : leftIsPressed = false; break;
case Keyboard.RIGHT : rightIsPressed = false; break;
case Keyboard.UP : upIsPressed = false; break;
case Keyboard.DOWN : downIsPressed = false; break;
}
}
function enterFrameHandler(e:Event):void {
vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
PlayerAuto.x += vx;
PlayerAuto.y += vy;
}
function generateParticles(event:Event):void
{
//so we don't do it every frame, we'll do it randomly
if(Math.random()*10 < 2){
//creating a new shape
var mcParticle:Shape = new Shape();
//making random dimensions (only ranges from 1-5 px)
var dimensions:int = int(Math.random()*5)+1;
//add color to the shape
mcParticle.graphics.beginFill(0x999999/*The color for shape*/,1/*The alpha for the shape*/);
//turning the shape into a square
mcParticle.graphics.drawRect(dimensions,dimensions,dimensions,dimensions);
//change the coordinates of the particle
mcParticle.x = int(Math.random()*stage.stageWidth);
mcParticle.y = -10;
//adding the particle to stage
particleContainer.stage.addChild(mcParticle);
}
//making all of the particles move down stage
for(var i:int=0;i<particleContainer.numChildren;i++){
//getting a certain particle
var theParticle:DisplayObject = particleContainer.getChildAt(i);
//it'll go half the speed of the character
theParticle.y += speed*.5;
//checking if the particle is offstage
if(theParticle.y >= 400){
//remove it
particleContainer.stage.removeChild(theParticle);
}
}
}
/**
* eventhandler voor als je op de tweede knop klikt
*/
private function loadScreenThree( event:MouseEvent ):void
{
// eerst opruimen!
cleanListeners();
// dan naar ander scherm
Project.instance.switchScreen( "derde" );
}
private function cleanListeners():void
{
drieButton.removeEventListener( MouseEvent.CLICK, loadScreenThree );
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.removeEventListener(Event.ENTER_FRAME, generateParticles);
}
}
}
I think the problem is here:
for(var i:int=0;i<particleContainer.numChildren;i++){
var theParticle:DisplayObject = particleContainer.getChildAt(i);
[...]
if (theParticle.y >= 400){
particleContainer.removeChild(theParticle);
}
}
You are removing children from the particleContainer while iterating through it. This is usually a bad idea because once you remove an item from the beginning, you are changing the index of all following items.
One solution is to loop backwards, so when you remove something the only items changed are the ones that you already checked.
for(var i:int=particleContainer.numChildren - 1; i>=0; i--){
And the rest keeps the same.

Movieclip not being removed from array or stage - AS3 - CS6

my enemy movie clips are not being removed from the stage on collision/hitTestObject with the bullet movie clip, the bullets are being removed though, only the enemies that are not. Appreciate all help with this. Thanks you guys, your a brilliant community here, respect you loads.
I used the same code that made the bullet movie clips be removed for the enemies (though change changing the vars etc accordingly).
My code for Main.as and Enemy.as is here:
Main.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
public var player:Player;
public var enemy:Enemy;
public var bulletList:Array = [];
public var mousePressed:Boolean = false; //keeps track of whether the mouse is currently pressed down
public var delayCounter:int = 0; //this adds delay between the shots
public var delayMax:int = 7; //change this number to shoot more or less rapidly
public var enemies:Array = [];
public function Main():void
{
player = new Player(stage, 320, 240);
stage.addChild(player);
//stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true); //remove this
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
for(var numBaddies=0; numBaddies<6;numBaddies++){
var enemy:Enemy = new Enemy(null);
enemy.x = numBaddies*50;
enemy.y = numBaddies*50
stage.addChild(enemy);
enemies.push(enemy);
}
}
public function loop(e:Event):void
{
if(mousePressed) // as long as the mouse is pressed...
{
delayCounter++; //increase the delayCounter by 1
if(delayCounter == delayMax) //if it reaches the max...
{
shootBullet(); //shoot a bullet
delayCounter = 0; //reset the delay counter so there is a pause between bullets
}
}
if(bulletList.length > 0)
{
for(var i:int = bulletList.length-1; i >= 0; i--)
{
bulletList[i].loop();
}
}
for(var h = 0; h<bulletList.length; ++h)
{
if(bulletList[h].hitTestObject(this)){
trace("player hit by baddie " + h);
}
}
for(var u:int=0; u<enemies.length; u++) {
Enemy(enemies[u]).moveTowards(player.x, player.y);
}
}
public function mouseDownHandler(e:MouseEvent):void //add this function
{
mousePressed = true; //set mousePressed to true
}
public function mouseUpHandler(e:MouseEvent):void //add this function
{
mousePressed = false; //reset this to false
}
public function shootBullet():void //delete the "e:MouseEvent" parameter
{
var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation, enemies);
bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true);
bulletList.push(bullet);
stage.addChild(bullet);
}
public function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved);
bulletList.splice(bulletList.indexOf(e.currentTarget),1);
}
}
}
Enemy.as
package {
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
public var bullets:Array;
public var stageRef:Stage;
private var enemyPositionX, enemyPositionY,xDistance,yDistance,myRotation:int;
public function Enemy(bulletList:Array) {
// constructor code
bullets = bulletList;
}
public function moveTowards(playerX:int, playerY:int){
xDistance = this.x - playerX;
yDistance = this.y - playerY;
myRotation = Math.atan2(yDistance, xDistance);
this.x -= 3 * Math.cos(myRotation);
this.y -= 3 * Math.sin(myRotation);
}
public function loop():void{
for(var i=0; i<bullets.length; ++i)
{
if(bullets[i].hitTestObject(this)){
trace("you killed enemy " + i);
removeSelf();
}
}
}
private function removeSelf():void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
And for reference so you can see how I used the same code to remove the movie clips, I'll add the Bullet.as too:
Bullet.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip
{
private var stageRef:Stage; //checks if the bullet leaves the screen borders
private var speed:Number = 10; //speed that the bullet will travel at
private var xVel:Number = 0; //current x velocity
private var yVel:Number = 0; //current y velocity
private var rotationInRadians = 0; //convenient to store our rotation in radians instead of degrees
public var allBaddies:Array;
//our constructor requires: the stage, the position of the bullet, and the direction the bullet should be facing
public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number, enemies:Array):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
this.rotation = rotationInDegrees;
this.rotationInRadians = rotationInDegrees * Math.PI / 180; //convert degrees to radians, for trigonometry
allBaddies = enemies;
}
public function loop():void //we don't need to include the "e:Event" because we aren't using an EventListener
{
for(var b=0; b<allBaddies.length; ++b)
{
if(allBaddies[b].hitTestObject(this)){
trace("bullet hit baddie " + b);
removeSelf();
}
}
xVel = Math.cos(rotationInRadians) * speed; //uses the cosine to get the xVel from the speed and rotation
yVel = Math.sin(rotationInRadians) * speed; //uses the sine to get the yVel
x += xVel; //updates the position
y += yVel;
//if the bullet goes off the edge of the screen...
if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
{
this.parent.removeChild(this); //remove the bullet
}
}
private function removeSelf():void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
Ok there are a couple of things I would change around, alongside a couple issues I noticed.
First, your Enemy class as far as I can tell never gets the stageRef variable set. Therefore in your Enemy class removeSelf() your stageRef will be null.
You can either pass the stage in through the constructor and set it (as you do in your bullet class), or you can set it after you instantiate in your enemies loop with enemy.stageRef = stage. Or you can add and event listener to your enemy class for Event.ADDED_TO_STAGE and when that happens your enemy will have a reference to the stage inherited.
Now, the real meat of the matter. You have 2 for loops testing for collision (aside from one in your Main class too, but I'll get to that). One is in your bullet class and one is in your enemy class. This is overkill and is difficult on performance if you start adding in lots of bullets and enemies. You really only need a single for loop (like the one in your bullet class), and when it detects a collision it calls the collided objects remove method. That would look something like this in your Bullet class:
public function loop():void //we don't need to include the "e:Event" because we aren't using an EventListener
{
for(var b=0; b<allBaddies.length; ++b)
{
if(allBaddies[b].hitTestObject(this)){
trace("bullet hit baddie " + b);
removeSelf();
allBaddies[b].removeSelf();
}
}
Although, for this to work you would need to set the removeSelf() in your Enemy class access to public:
public function removeSelf():void
{
//this is removing a listener not even added Enemy...
//removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
Then you can probably even eliminate your loop() from the Enemy class seeing as how all it does it detect collisions that will no longer be necessary.
Lastly as to why this might happened? Well it could be the bullet hitTest which is running first from your call in the main class: bulletList[i].loop(); This hitTest is being detected, then removing the bullet leaving your Enemy class without an object to hit test against. BUT! you are not calling the loop() from enemy anywhere in your main class, therefore that hit test will never run.
It's best to condense your collisions down. You are currently looping through your Bullet instances in the main class, calling their loop() (which tests for collisions), and then afterwards doing a for loop doing the collision test again. You could change this:
if(bulletList.length > 0)
{
for(var i:int = bulletList.length-1; i >= 0; i--)
{
bulletList[i].loop();
}
}
//this is no longer needed...
/*for(var h = 0; h<bulletList.length; ++h)
{
if(bulletList[h].hitTestObject(this)){
trace("player hit by baddie " + h);
}
}*/
There is no reason to test the collision in the Bullet class AND in the Main class loop(e:Event).
Hope this information helps! Good luck :)

AS3 mismatch while accessing object in an array

I have encountered quite a challenging one and I'd love to get some support.
Here is the scenario :
The main Game class instances the Level1 Class in charge for spawning enemies through nested For loops and push them to an array.
It then checks for collisions between the Bullet and Enemy and if it find a collision it calls a method in the Enemy class that removes removeChild and Splice itself from the array.
The thing is it works for the first few enemies, and then it will pick the wrong Enemy to destroy, and stop completely to function.
I tried using indexOf to be sure I am referring to the right object, but to no avail.
I think the Pslice and removeChild are pointing to different objects.
This mess happended when I moved the removeChild and splice from the Game Class to the Enmy class
Link to the work in progress : https://dl.dropboxusercontent.com/s/69hcmzygnkx7h1e/space_shooter.swf
I'd like some help on this one.
Thank you !!!
Main class : Game.AS
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.*;
import flash.geom.Point;
public class Game extends MovieClip
{
public var _instance : Game;
public var player:Player;
public static var level1:Level1;
public var bullet:Bullet;
private var bullets_arr:Array;
var fire_on : Boolean;
var fire_counter : int;
public function Game()
{
level1=new Level1(this.stage);
player = new Player ;
addChild(player);
player.y = 600;
bullets_arr = [];
addEventListener(Event.ENTER_FRAME,Main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
function mouseDownHandler($e:MouseEvent):void
{
fire_on = true;
}
function mouseUpHandler($e:MouseEvent):void
{
fire_on = false;
fire_counter = 0;
}
function fire():void
{
bullet = new Bullet ;
addChild(bullet);
bullet.x = player.x;
bullet.y = player.y - 32;
bullets_arr.push(bullet);
}
public function Main(e: Event):void
{
player.x = mouseX;
if (bullets_arr)
{
for (var m:int = 0; m < bullets_arr.length; m++)
{
bullets_arr[m].y -= 20;
if(Game.level1.enemies_arr)
{
for (var n:int = 0; n < Game.level1.enemies_arr.length; n++)
{
if (Game.level1.enemies_arr[n].hitTestObject(bullets_arr[m]))
{
if(bullets_arr[m].parent)
{
bullets_arr[m].parent.removeChild(bullets_arr[m]);
bullets_arr.splice(bullets_arr[m],1);
Game.level1.enemies_arr[n].Damage(10, Game.level1.enemies_arr[n]);
}
}
}
}
}
}
if(fire_on)
{
fire_counter++;
if(fire_counter == 01)
{
fire();
}
else if(fire_counter >2)
{
fire_counter =0;
}
}
}
}
}
Level1.as where the enemies are spawned and pushed to the array.
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Level1 extends MovieClip
{
var i:int;
var j:int;
var frame :int;
public var enemy:Enemy;
public var enemies_arr:Array;
public function Level1(target:Stage)
{
frame = 0;
enemies_arr = [];
for (var i:int = 0; i < 5; i++)
{
for (var j:int = 0; j < 3; j++)
{
enemy = new Enemy;
enemy.x = j*100 + 260;
enemy.y = i*40 - 150;
target.addChild(enemy);
enemies_arr.push(enemy);
trace(enemy.parent);
}
}
}
}
}
The Enemy class Enemy.AS
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
var Health : int;
function Enemy()
{
Health =2;
}
public function Damage(Damage:int, enemyHit:Enemy)
{
Health -= Damage;
if (Health <1)
{
Die(enemyHit);
}
}
private function Die(enemyHit:Enemy)
{
if(enemyHit.parent)
{
this.parent.removeChild(this);
Game.level1.enemies_arr.splice(Game.level1.enemies_arr.indexOf(enemyHit,1));
}
}
}
}
You should traverse both Game.level1.enemies_arr and bullets_arr backwards. The point is, splice() shortens the array, shifts the elements that are in greater positions than the spliced one(s) to lesser indexes, and the loop counter is not automatically adjusted. The error is pretty common, but is often overlooked. Also, with bullets_arr you can get out of the array causing a 1009 error if your last bullet out of bullets_arr will hit an enemy.
A small nitpick: You are checking for the array's existence within a loop, and for another array's existence once within the enter frame listener. In fact you should either initialize them with at least new Array() or [] before you add an event listener to the Main object, or wherever this is assigned to, or check like if (!bullets_arr) bullets_arr=new Array(); and leave it at that, so one check of the array's existence will be needed.
public function Main(e: Event):void
{
player.x = mouseX;
if (!bullets_arr) bullets_arr=new Array();
if (!Game.level1.enemies_arr) throw new Error('Hey programmer, initialize your arrays!');
// ^ bad practice to throw exceptions in listeners, but if you get one, you've coded something wrongly.
for (var m:int=bullets_arr.length-1;m>=0;m--) {
var bm:Bullet=bullets_arr[m]; // TODO fix type
// the local variable is a cleaner and faster approach
bm.y-=20;
for (var n:int=Game.level1.enemies_arr.length-1;n>=0;n--) {
if (!bm) break; // bullet was destroyed, why checking more enemies vs that?
if (Game.level1.enemies_arr[n].hitTestObject(bm)) {
bm.parent.removeChild(bm);
bullets_arr.splice(m,1); // splice is done by index, not by object
bm=null; // oops, missed this. The bullet hit something and is now lost
// so we null the local var, so we can break out the loop.
Game.level1.enemies_arr[n].Damage(10, Game.level1.enemies_arr[n]);
}
}
}
// rest of your code follows here
}
I finally found the problem, it was very stupid of me :
I just mistyped the Die function in the Enemy class :
I have written :splice(Game.level1.enemies_arr.indexOf(enemyHit,1)) instead of splice(Game.level1.enemies_arr.indexOf(enemyHit),1)
Anyway I learnt a lot of think by trying to fix this error.
Thanks

Adding movie clips into 2D array

package
{
import flash.events.Event;
import flash.display.MovieClip;
// class
public class GameGrid extends MovieClip
{
private var gameHeight:Number = 600;
private var gameWeight:Number = 800;
private var gridHeight:Number = 50;
private var gridWeight:Number = 50;
private var rowNumber:int = 12;
private var columnNumber:int = 16;
private var backgroundGrid:Array = new Array(12,16);
private var foregroundGrid:Array = new Array(12,16);
function GameGrid(){
}
function addBackGrid(rowN:int,colN:int,mcObject:MovieClip)
{
backgroundGrid[rowN,colN].push(mcObject);
}
function addForeGrid(rowN:int,colN:int,mcObject:MovieClip)
{
foregroundGrid[rowN,colN].push(mcObject);
}
function calculateRowDiff(rowA:int,rowB:int):Number
{
return Math.abs(rowA-rowB);
}
function calculateColDiff(colA:int,colB:int):Number
{
return Math.abs(colA-colB);
}
function calculateCorDiff(colA:int,colB:int,rowA:int,rowB:int):Number
{
return Math.sqrt((calculateRowDiff(rowA,rowB) * calculateRowDiff(rowA,rowB)) + (calculateColDiff(colA,colB) * calculateColDiff(colA,colB)));
}
// add to stage
function paintbackgroundGrid()
{
for (var i:int=0; i<16; i++)
{
for (var j:int=0; j<12; j++)
{
MovieClip(backgroundGrid[i,j]).x = i * 50;
MovieClip(backgroundGrid[i,j]).y = j * 50;
stage.addChild(MovieClip(backgroundGrid[i,j]));
}
}
}
}
}
So what this GameGrid class do is to hold an Array of grids(or tiles which extends MovieCLip) that will be added to the main stage and will call the initializeItem function.
function InitializeItem(e:Event)
{
var gamemap = new GameGrid();
var mc:MovieClip = new MainCharacter();
gamemap.addBackGrid(1,1,mc);
gamemap.paintbackgroundGrid();
//trace("Year: "+gameTime.gameYear+" Month: "+gameTime.gameMonth+" Day: "+gameTime.gameDay+" "+gameTime.gameHour+":"+gameTime.gameMinute+":"+gameTime.gameSecond);
}
The initializeItem should create an instance of gamegrid, and add movieclips to their respective locations(stored using array) and display them.
and this is the error stacktrace:
ReferenceError: Error #1069: Property 1 not found on Number and there is no default value.
at GameGrid/addBackGrid()
The debugger suggest that the error came from the line backgroundGrid[rowN,colN].push(mcObject);
Is there a way I can hold a 2d array movieclips? I'm new to AS3 and it looks very similar to JAVA, what am I missing?
Try this
private var backgroundGrid = [];
function addBackGrid(rowN:int,colN:int,mcObject:MovieClip) {
if (backgroundGrid[rowN] == null) {
backgroundGrid[rowN] = [];
}
backgroundGrid[rowN][colN] = mcObject;
}
In as3, following code means create a array and the array contains two elements, one is 12, and the other is 16.
private var backgroundGrid:Array = new Array(12,16);

AS3 array of movieclip buttons with fluid animation and AT state

I wanted to setup an array of movieclip buttons to navigate across my timeline via labels, this is where it went pear shaped.
Having spent three days reading and attempting most online solutions I couldn't find a method which worked 100% without failing in some way or another.
I've had some joy with the method below having seen a blog entry covering different ways to call frames etc and which highlighted the bugbear below :
clipArray[i].mouseChildren = false; //Hidden bugbear
I've added the full code below so hopefully it may help anyone else who similarly nearly resorted to hari-kari in trying this.
import flash.events.MouseEvent;
import flash.events.Event;
var clipArray:Array = [btn_1,btn_2]; // Movieclip's called btn_1 etc...
var destArray:Array = ["page_1","page_2"]; Labels on timeline...
for (var i:int = 0; i < clipArray.length; i++) {
clipArray[i].buttonMode = true; // Define Button from Movie Clip
clipArray[i].useHandCursor = true; // Enable HandCursor over clip
clipArray[i].mouseChildren = false; // Define clip as single denomination
clipArray[i].addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
clipArray[i].addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
clipArray[i].addEventListener(Event.ENTER_FRAME, frameHandler);
clipArray[i].addEventListener(MouseEvent.CLICK,clickHandler, false, 0, true);
}
function clickHandler(event:MouseEvent):void {
for (var i:int = 0; i < clipArray.length; i++) {
if (event.currentTarget == clipArray[i]) {
this.gotoAndStop(destArray[i]);
clipArray[i].mouseEnabled = false;
clipArray[i].useHandCursor = false;
clipArray[i].alpha = 0.5;
} else {
clipArray[i].mouseEnabled = true;
clipArray[i].useHandCursor = true;
clipArray[i].alpha = 1;
}
}
}
function mouseOverHandler(e:MouseEvent){
e.target.onOff = true;
}
function mouseOutHandler(e:MouseEvent){
e.target.onOff = false;
}
function frameHandler(e:Event){
if(e.target.onOff){
e.target.nextFrame();
} else {
e.target.prevFrame();
}
}
This works fine, now however my understanding of whether it is 'good' code or not is an issue, if this could be improved in any way I'd like to know why and how as the problem with learning AS3 from 2 is that often you use code having seen it online without fully grasping the detail.
Tentatively, I'm pleased as this proved to be a nightmare to find or to resolve and hope it helps anyone else in a similar state of mind.
Adding MovieClip buttons with fluidity and which cancel out from an array became a three day mission when you're learning...
You might find you have more freedom if you put all of this in a class and use the Tween class to travel to your 'labels' instead of the timeline. It would mean that you would be able to remove your event listeners until your animation has finished.
Also it looks like you might be better off using the MouseEvent.ROLL_OVER and MouseEvent.ROLL_OUT
If I'm honest I don't know what some of your methods want to do without seeing your whole project, but I've quickly written a class for you. I've replaced animating between pages with just having your buttons animate to a ramdom location. (Remember to export your MovieClips you create with the IDE into actionscript, giving them the class name Button01, Button02 etc...). I hope this sends you in the right direction :)
package com
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
public class Main extends MovieClip
{
private var btn_1:Button01 = new Button01;
private var btn_2:Button02 = new Button02;
private var clipArray:Array = new Array();
private var xAxis:Number = 0;
private var yAxis:Number = 0;
private static const WIDTH:int = 300;
private static const HEIGHT:int = 250;
private var tweenButton:Tween;
public function Main()
{
makeArrays();
addChildren();
setEventListeners()
}
private function makeArrays():void
{
for(var i:uint=0; i<2; i++)
clipArray.push(this['btn_'+(i+1)]);
}
private function addChildren():void
{
for(var i:uint=0; i<clipArray.length; i++){
addChild(clipArray[i]);
clipArray[i].x = WIDTH*Math.random();
clipArray[i].y = HEIGHT*Math.random();
}
}
private function setEventListeners():void
{
for (var i:uint=0; i<clipArray.length; i++) {
clipArray[i].buttonMode = true; // Define Button from Movie Clip
//clipArray[i].useHandCursor = true; // Enable HandCursor over clip
//clipArray[i].mouseChildren = false; // Define clip as single denomination // DON'T NEED THIS WITH ROLL_OVER
clipArray[i].addEventListener(MouseEvent.ROLL_OVER, mouseOverHandler);
clipArray[i].addEventListener(MouseEvent.ROLL_OUT, mouseOutHandler);
clipArray[i].addEventListener(Event.ENTER_FRAME, frameHandler);
clipArray[i].addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
}
}
private function tweenButtons(e:Event):void
{
var dispObj = e.currentTarget as DisplayObject;
dispObj.removeEventListener(MouseEvent.CLICK, clickHandler)
tweenButton = new Tween(dispObj, 'x', Regular.easeIn, dispObj.x, WIDTH*Math.random(), 1, true);
tweenButton = new Tween(dispObj, 'y', Regular.easeIn, dispObj.y, HEIGHT*Math.random(), 1, true);
tweenButton.addEventListener(TweenEvent.MOTION_FINISH, reattachEventListener);
}
private function reattachEventListener(e:Event):void
{
tweenButton.removeEventListener(TweenEvent.MOTION_FINISH, reattachEventListener);
for (var i:uint=0; i<clipArray.length; i++) {
if(!(hasEventListener(MouseEvent.CLICK)))
clipArray[i].addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
}
}
private function clickHandler(e:MouseEvent):void
{
for (var i:uint=0; i<clipArray.length; i++) {
if (e.currentTarget == clipArray[i]) {
tweenButtons(e);
clipArray[i].buttonMode = false
clipArray[i].alpha = 0.5;
} else {
clipArray[i].buttonMode = true;
clipArray[i].alpha = 1;
}
}
}
private function mouseOverHandler(e:MouseEvent):void // I HAVE NO IDEA WHAT YOU'RE TRYING TO DO HERE
{
e.target.onOff = true;
}
private function mouseOutHandler(e:MouseEvent):void // I HAVE NO IDEA WHAT YOU'RE TRYING TO DO HERE
{
e.target.onOff = false;
}
private function frameHandler(e:Event):void // I HAVE NO IDEA WHAT YOU'RE TRYING TO DO HERE
{
if(e.target.onOff){
e.target.nextFrame();
}else{
e.target.prevFrame();
}
}
}
}

Resources