Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-02-2010, 11:12 PM   #1
dansaDisco
Registered User
 
Join Date: Aug 2010
Posts: 7
Default [AS2] Vertical Shooting system

Hello again!
Since you guys helped me out last time, I thought you may be able to give me a hand on this one too.

I'm making a simple 2 player game, kind of like pong but instead of a bouncing ball, you simply shoot "bullets" and if you hit the other players platform, you get a point.

I've made the start of the projectile system, though I'm having some problems.
I really don't understand how i should move multiple bullets at the same time, right now only one bullet moves, and if you fire again while it's moving, it simply starts over.

I guess that I have to do it with loops, but I've tried for hours without getting it to work.

NOTICE: I'd rather get an explanation on HOW to do it, rather than you doing it FOR me. Since it's school work, and I'm supposed to learn something


Here's the code:

ActionScript Code:
Key.addListener(this); function onKeyUp():Void {     if (Key.getCode() == Key.SPACE)     {         {             i++;             _root.attachMovie("bullet","bullet" + i,this.getNextHighestDepth());             _root["bullet" + i]._x = circle._x + 20;             _root["bullet" + i]._y = circle._y + 15;             onEnterFrame = function ()             {                 _root["bullet" + i]._x += 15;                 if (_root["bullet" + i].hitTest(square))                 {                     trace("hit");                     _root["bullet" + i].removeMovieClip();                 }             }         }     } }
dansaDisco is offline   Reply With Quote
Old 09-03-2010, 10:06 AM   #2
neilmmm
Senior Member
 
neilmmm's Avatar
 
Join Date: Oct 2005
Location: dorset
Posts: 1,637
Default

your problem is your onEnterFrame ... on enterFrame need to be asigned to a movieclip ... is unassigned they default to _root ... a movieclip can only really have one on enterframe

at the moment your onenterframe is unassigned try something like

_root["bullet" + i]. onEnterFrame = function ()

to assign it to _root["bullet" + i]
neilmmm is offline   Reply With Quote
Old 09-03-2010, 03:36 PM   #3
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

When you do
ActionScript Code:
onEnterFrame = function () { ... }
You are defining the function within that block, not calling it. If you use the variable "i" inside the function, it will use the value of "i" at the time the function is called, not the time it is defined.

Neil's idea of assigning each bullet its own onEnterFrame function will allow you to reference the bullet inside the function using "this" rather than rely on the value of "i."
rrh is offline   Reply With Quote
Old 09-03-2010, 05:08 PM   #4
neilmmm
Senior Member
 
neilmmm's Avatar
 
Join Date: Oct 2005
Location: dorset
Posts: 1,637
Default

i should have added because of your approach .. you over writing yourself and that is why only one ...the last one...is moving

and i absolutely agree with rrh
neilmmm is offline   Reply With Quote
Old 09-03-2010, 05:50 PM   #5
Dreadnok
Registered User
 
Join Date: Aug 2010
Posts: 16
Default

Related question.
I've been working on a platform shooter and I'm having a related problem.

In the primer code(disco's code modified), previously fired bullets change direction if you turn and fire in another direction.

How can you make bullet[i] travel only in the direction it was fired?

primer code
ActionScript Code:
var DirectionX = 30; var DirectionY = 0; var i = 0; this.onEnterFrame = function () { if (Key.isDown(39)) { Circle._x = Circle._x + 5; Circle._rotation = 0; DirectionX = 30; DirectionY = 0; } if (Key.isDown(37)) { Circle._x = Circle._x - 5; Circle._rotation = 180; DirectionX = -30; DirectionY = 0; } if (Key.isDown(38)) { Circle._rotation = -90; Circle._y = Circle._y - 5; DirectionX = 0; DirectionY = -30; } if (Key.isDown(40)) { Circle._rotation = 90; Circle._y = Circle._y + 5; DirectionX = 0; DirectionY = 30; } if (Key.isDown(32)) { ++i; _root.attachMovie("Bullet", "Bullet" + i, 1000 + i); _root["Bullet" + i]._x = _root.Circle._x + DirectionX; _root["Bullet" + i]._y = _root.Circle._y + DirectionY; } _root["Bullet" + i].onEnterFrame = function () {       if (i == 100){i = 0;} this._x = this._x + DirectionX; this._y = this._y + DirectionY; if (this._x < 0 || this._x > 500 || this._y < 0 || this._y > 400) {this.removeMovieClip();} }; };
Dreadnok is offline   Reply With Quote
Old 09-03-2010, 06:29 PM   #6
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

Dreadnok: You're right that it's a similar problem. Take everything I said about the value of "i" at the time of execution, not definition, and apply it to "DirectionX" and "DirectionY."
rrh is offline   Reply With Quote
Old 09-03-2010, 06:06 PM   #7
dansaDisco
Registered User
 
Join Date: Aug 2010
Posts: 7
Default

Do you guys mean something like this?:
Because when I execute this code, it doesn't spawn any bullets anymore, and wherever I aim, I get the "hit" trace

any ideas?


ActionScript Code:
Key.addListener(this); function onKeyUp():Void {     if (Key.getCode() == Key.SPACE)     {         {             i++;             _root.attachMovie("bullet","bullet" + i,this.getNextHighestDepth());             _root["bullet" + i]._x = circle._x + 20;             _root["bullet" + i]._y = circle._y + 15;             _root["bullet" + i].onEnterFrame = function()             {                 this._x += 15;                 this.hitTest(square);                 {                     trace("HIT");                     this.removeMovieClip();                 }             }         }     } }
dansaDisco is offline   Reply With Quote
Old 09-03-2010, 06:11 PM   #8
neilmmm
Senior Member
 
neilmmm's Avatar
 
Join Date: Oct 2005
Location: dorset
Posts: 1,637
Default

i think it is spawning but you have not written your hitTest correctly

Code:
this.hitTest(square)
it needs to go in an if satement

somehting like

Code:
if(this.hitTest(square)){


// something happens

}
so i think it is attaching and then removing

comment out removeMovie clip and seeing if i am right then adjust your hitTest
neilmmm is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:17 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.