View Full Version : [AS3] Shooting system
Geoff666
10-02-2009, 02:16 PM
Hi,
I'm creating a simple shooting-system, but I'm kinda stuck now.
The idea is to create a bullet child when i hit the spacebar, and in the bullet class, it will define it's own x and y-axis, which is the same as the spaceship.
However, I get errors when I tell it that the bullet's.x is the same as player.x (player = spaceship).
Does anyone know the solution? I'm sure i forgot something really silly here.
spaceship:
private function movePlayer(event:KeyboardEvent)
{
if (event.keyCode == 39) x+=maxspeed;
if (event.keyCode == 37) x-=maxspeed;
if (event.keyCode == 38) y-=maxspeed;
if (event.keyCode == 40) y+=maxspeed;
if (event.keyCode == 32){
var bullet:MovieClip= new Bullet();
this.addChild(bullet);
}
}
bullet:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip
{
public function Bullet()
{
addEventListener(Event.ENTER_FRAME,MoveBullet);
x = player.x;
y = player.y;
}
public function MoveBullet(event:Event)
{
y--;
//if (y < 0) removeChild(bullet);
}
}
}
Thanks in advance,
Geoff.
krayzeebean
10-02-2009, 08:08 PM
First of all, you should post the whole error message you get. Just saying "I'm getting errors" doesn't tell us anything about what the cause of the error might be.
In your case though, it looks like you never defined "player" in the Bullet class. You need to give the bullet a reference to the player or it won't know what player is or where player.x is.
Geoff666
10-02-2009, 08:15 PM
I'm sorry, I totally forgot that.
The error message is "Use of a non-defined property of player"
I guess you're right there then, how do I fix this?
krayzeebean
10-02-2009, 08:25 PM
Add a parameter to the Bullet class's constructor:
public function Bullet(player:MovieClip)
{
addEventListener(Event.ENTER_FRAME,MoveBullet);
x = player.x;
y = player.y;
}
And the player passes a reference to itself when it creates a new bullet:
var bullet:MovieClip= new Bullet(this);
Geoff666
10-02-2009, 08:41 PM
That does the trick.
However, my bullet spawns WAAAY far from my spaceship, while i centered them both..?
krayzeebean
10-02-2009, 10:30 PM
And now you get to learn about the display list and parent/child relationships :)
You really should find a good AS3 book, I found Learning ActionScript 3.0: A Beginner's Guide by Rich Shupe to be really good when I was starting out.
The problem is you are adding the bullet as a child of the player, so its x/y position is relative to the parent's registration point (0,0). The player is also a child of some other display object (I'm assuming the document class?) so it's position is relative to that. Putting the bullet at the same x and y position as the player would probably work if you were adding the bullet to the same parent as the player rather than the player itself, but it also depends on how the player artwork is placed within the MovieClip.
Having the bullet a child of the player is going to cause other problems as well; if you move the ship left, the bullet will also move left.
Geoff666
10-03-2009, 12:59 PM
*totally confused*
if (event.keyCode == 32){
var bullet:MovieClip= new Bullet(this);
this.addChild(bullet);
}
From what I understood, this initializes a bullet every time i hit the spacebar.
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip
{
public function Bullet(player:MovieClip)
{
addEventListener(Event.ENTER_FRAME,MoveBullet);
x = player.x;
y = player.y;
}
public function MoveBullet(event:Event)
{
y-=20;
//if (y < 0) removeChild(bullet);
}
}
}
And this should set the bullet's initializing position to the player's x and y, right?
I'll definatly get my hands on some books, but to be honest; I'm pretty broke and my time is running out (this is a school thing).
Could you give me an example of how you'd do this?
krayzeebean
10-03-2009, 07:39 PM
It's not putting it at the player's x and y position, it's using the same x value and y value. Because you're adding it as a child of the player, the bullet position is relative to the player, not the stage. If the player is at 350,500 on the stage, and you add the bullet to the player using 350,500 then the bullet will be 350 pixels to the left of the player and 500 pixels below the player.
So you should either add the bullet at 0,0 (directly on top of the player, assuming the artwork is centered to the registration point) or you add it to the same parent that the player has. Since adding the bullet as child of the player will result in other unwanted consequences, you should add the bullet to the player's parent. This would be better handled in the document class instead of the player class, but a simple way of doing it with the code you've already written is this:
this.parent.addChild(bullet);
Everything else in your code can stay the same.
Geoff666
10-03-2009, 11:04 PM
That was very clear, thanks alot!
Without your help I'd still be searching tutorials which lead me nowhere :p
Just a simple question tho, what does that 'this' mean/do?
krayzeebean
10-04-2009, 06:36 PM
"this" is a reference to the class instance. So in your player class, when you use "this" it's referring to itself (the player), in your bullet class it's referring to the bullet instance. You usually don't need to use it but it but some people prefer to always use it for the sake of clarity. In your player class you could use parent.addChild instead of this.parent.addChild.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.