View Full Version : Bullet Array in As3.0
womposan
August 8th, 2008, 09:27 AM
Need some help creating a "bullet array" in AS3.0. if someone has a good way doing this, any help would be appreciated!
therobot
August 8th, 2008, 01:33 PM
Need some help creating a "bullet array" in AS3.0. if someone has a good way doing this, any help would be appreciated!
You'll add your bullets to the array by using push();
myArray.push( newBullet );
You'll remove your bullets from the array using splice( index, count)
myArray.splice( 4, 1 );
The Help files in Flash do a better job of explaining these, but they are pretty easy to use.
womposan
August 8th, 2008, 08:07 PM
Got it, thanks!
But i stumbled into a new problem: where should i create this array? If i create it to Bullet class, how can i communicate with it from lets say, Player class, if needed? Or should i create the array to timeline?
substance
August 9th, 2008, 05:56 AM
The way I go about it is Player->Weapon->Ammo. Then link them individually.
Like say my character picks up a power up that turns his bullets into fire bullets:
player.weapon.ammo.fire = true;
Or you can go backwards, for example if a bullet hits someone:
if (ammo.weapon.player == enemyTeam) then HIT
womposan
August 10th, 2008, 09:34 PM
Got the bulletarray to work, sort of.
However, i still got one little problem (partly offtopic, sorry about that). Don't know what's causing this. The case is: im creating a top-down shooter with bullets that can move along an angle. When shooting, the bullets are created and they go nicely into the bulletarray, but they won't move at stage. They just chill at 0,0.
Player shoots bullets when clicked with mouse on stage:
Player.as
public function mouseClickHandler():void {
arguments;
var b:Bullet = new Bullet();
b.x = this.x;
b.y = this.y;
b.go();
stage.addChild(b);
}
(go-function is just for pausing game events, if needed later on...)
...And the bullet movement happens in Bullet class:
Bullet.as (ill put the whole bullet-class here, because i think the problem is there)
public class Bullet extends MovieClip {
public var _go:Boolean;
public var _speed:Number = 12;
public var _angle:Number;
public var radians:Number = deg2rad(_angle);
public var _xVel:Number = Math.cos(radians) * _speed;
public var _yVel:Number = Math.sin(radians) * _speed;
public var myBulletArray:Array = new Array();
public function Bullet() {
this.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
myBulletArray.push(this);
}
public function onLoop(evt:Event):void {
if (_go) {
for (var i:int = 0; i<myBulletArray.length; i++) {
myBulletArray[i].x += _xVel;
myBulletArray[i].y += _yVel;
}
}
}
function deg2rad(deg:Number):Number {
return deg * Math.PI / 180;
}
public function go():void {
_go = true;
}
}
Please help! :(
therobot
August 11th, 2008, 12:08 AM
Got the bulletarray to work, sort of.
However, i still got one little problem (partly offtopic, sorry about that). Don't know what's causing this. The case is: im creating a top-down shooter with bullets that can move along an angle. When shooting, the bullets are created and they go nicely into the bulletarray, but they won't move at stage. They just chill at 0,0.
Player shoots bullets when clicked with mouse on stage:
Player.as
public function mouseClickHandler():void {
arguments;
var b:Bullet = new Bullet();
b.x = this.x;
b.y = this.y;
b.go();
stage.addChild(b);
}
(go-function is just for pausing game events, if needed later on...)
...And the bullet movement happens in Bullet class:
Bullet.as (ill put the whole bullet-class here, because i think the problem is there)
public class Bullet extends MovieClip {
public var _go:Boolean;
public var _speed:Number = 12;
public var _angle:Number;
public var radians:Number = deg2rad(_angle);
public var _xVel:Number = Math.cos(radians) * _speed;
public var _yVel:Number = Math.sin(radians) * _speed;
public var myBulletArray:Array = new Array();
public function Bullet() {
this.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
myBulletArray.push(this);
}
public function onLoop(evt:Event):void {
if (_go) {
for (var i:int = 0; i<myBulletArray.length; i++) {
myBulletArray[i].x += _xVel;
myBulletArray[i].y += _yVel;
}
}
}
function deg2rad(deg:Number):Number {
return deg * Math.PI / 180;
}
public function go():void {
_go = true;
}
}
Please help! :(
You should put your bullet Array in a parent of the Bullet class, otherwise you defeat the purpose of having an array, since you'd be making XX instances of a Bullet, each with their own array. Also, I don't see in your code where you are calculating the angle and such for the bullet. I know you have it set up in the public variables, but I don't see where you are giving the bullet an angle, which would probably cause it to not work.
Did you try tracing the bullet's _xVel & _yVel?
womposan
August 12th, 2008, 09:09 AM
You're so true about the array in Bullet class :). I moved it to Bullet class's parent, which is stage. So now i'm facing again THE problem, that i cannot reach myBulletArray from Bullet class. Reaching stage or root from class in as3.0 is so d*mn hard :/ so i have tried to avoid it as much as possible...
I tried to add a listener to Bullet class constructor to see if it is added to stage before refering to it, by:
Bullet.as
public function Bullet() {
this.addEventListener(Event.ADDED_TO_STAGE, createStageListener);
...
}
...
function createStageListener(event:Event):void {
stage.myBulletArray.push(this);
this.removeEventListener(Event.ADDED_TO_STAGE, createStageListener);
}
..but it did'nt work. (Not sure if the removeEventListener's syntax is correct, but it's irrelevant at this point).
I also tried to reach stage by creating TopLevel class, extending it in Bullet class and refering from Bullet class to stage with "TopLevel.stage" (from senocular's tips: http://www.kirupa.com/forum/showthread.php?p=1952513#post1952513), but didn't get it to work either.
If someone has some solid solutions for this: HELP!!
bluemagica
August 13th, 2008, 01:41 AM
Hmm, i have been watching this topic for the past few days, but since i myself don't know as3, i didn't feel like saying anything!
Logically, the bulletArray, and such, have to be persistent, so putting them inside the bullet is really pointless, now what object is persistent throughout the game, or atleast till bullets are used? Hmm lets see.........maybe the 'hero' himself??
Yes, i am saying to put the bullet array inside the hero....extend bullet class from within hero and let him control the bullets, afterall 'he' is creating them, so he should take care of them too!!
As for what you were trying to do with stage...when you create a array on stage, i don't think the array becomes a stage property, and as as3 says "outside looking in, not inside looking out", you can't access the array. however, if you put the bullet array, and the mover code on stage to be handled from timeline......it should work!
Oh and another tiny thing you should keep in mind is that, till a object is added to the display list, it cant access its stage property, so to access stage you shouldn't call it from the constructor function but a separate init function! like var my_stage:Stage = new Sprite().stage;
womposan
August 13th, 2008, 08:23 AM
Thanks for your advices! I decided to trash the whole "let's not put any code into timeline"-idea. By creating game's "core code" into the timeline, problems have been solved. Bullet Array works now perfectly.
Keeping things simple :proud:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.