PDA

View Full Version : [AS2] Getting a charcter to shoot.


Hackor Pickel
06-24-2009, 06:49 PM
Ok, i have this platformer i am making, and i want the character to shoot a "bullet" from his gunwhen the x is pressed, so far i have this, the bullet is showing up on the stage, but not on the character's x and y, and isn't moving.

var heroDir = "right";
var bulletx = _root.level_container.hero._y;
var bullety = _root.level_container.hero._x;

onEnterFrame = function () {
if (Key.isDown(37))
{
xspeed = xspeed - speed;
walking = true;
_root.level_container.hero.gotoAndStop(2);
heroDir = "right";
}
if (Key.isDown(39))
{
xspeed = xspeed + speed;
walking = true;
_root.level_container.hero.gotoAndStop(1);
heroDir = "left";
}
}
_root.level_container.onEnterFrame = function () {
if(Key.getCode() == 88) {
_root.attachMovie("bullet", "bullet_mc", _root.getNextHighestDepth());
_root.attachMovie("bullet", "bullet_mc", _root.getNextHighestDepth(), {_x:bulletx, _y:bullety});
}
if (heroDir == "left") {
_root.level_container.bullet_mc._x +=5;
}
if (heroDir == "right") {
_root.level_container.bulet_mc._x -=5;
}

Can somebody help? or just show me what to change? I want to learn as much as possible from this.

pradvan
06-24-2009, 09:58 PM
From a quick glance I can see that you're not giving the new bullet a unique name:

_root.attachMovie("bullet", "bullet_mc", _root.getNextHighestDepth());

change that to

_root.attachMovie("bullet", "bullet_mc"+_root.getNextHighestDepth(), _root.getNextHighestDepth());

Hackor Pickel
06-24-2009, 10:28 PM
I changed it, but it's still not working. :/

pradvan
06-24-2009, 11:53 PM
Ok, start debugging by tracing heroDir, bulletx, bullety, etc, in your onEnterFrame. Also trace bullet_mc's x & y.

I also noticed that you're attaching your bullet to _root but calling it in a container when setting x & y

bluemagica
06-25-2009, 08:49 AM
"bullet" spelt wrongly in hero right! And you are trying to move it from a container where it does not exist..... the easier way is to use a reference

var bullet_ref = _root.attachMovie(....whatever);

//later
bullet_ref._x+=5;


Also i suggest using an array to keep track of the bullets!

pradvan
06-25-2009, 02:39 PM
So what did tracing reveal?