The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumFiring a bullet

18 9612
markzi
offline
markzi
103 posts
Nomad

I made the gun that rotates and the bullet as well, but I do not know the functions necessary to fire the bullet. Please help.

  • 18 Replies
Badboy520
offline
Badboy520
116 posts
Nomad

What game is it... That'll help!

Dannydaninja
offline
Dannydaninja
948 posts
Nomad

Badboy, Markzi isn't talking about a game, but how programming a game.
First off, AS2 or AS3?

markzi
offline
markzi
103 posts
Nomad

AS2

arobegamr
offline
arobegamr
130 posts
Nomad

Is this a top-down video game?

Dannydaninja
offline
Dannydaninja
948 posts
Nomad

All you have to do is to get a movie clip to shoot projectiles from the player movie clip toward to the Mouse X and Y position.

arobegamr
offline
arobegamr
130 posts
Nomad

I don't mean to be rude, Dannydaninja, but you're being far too general.

Here is a way:

First, you need to set a variable so that flash can render more than one bullet at a time. On the main stage actions panel, enter:
bulletNum=0

Next, create a movie clip to serve as the player, and in the library panel, give it the linkage, &quotlayer" (right click the movie clip in the library panel, and scroll to linkage. Select "export for actionscript&quot
Enter this into the main stage actions panel:
var player:MovieClip = _root.attachMovie(&quotlayer",&quotlayer",1)
player._x=550/2
player._y=400/2

For the generic 550x400 stage used by flash, this will place the player in the center of the stage.

Now create a new movie clip to serve as the bullet. Give this the linkage "bullet"

Now you need to create a mouse listener to detect if the player has clicked. Enter this in the main stage actions panel:
var mouseListener:Object = new Object()
mouseListener.onMouseDown=function(){
var bullet:MovieClip = _root.attachMovie("bullet","bullet"+bulletNum,100+bulletNum)
bulletNum++
bullet._x=player._x
bullet._y=player._y
bullet.speed=5
bullet.rads=Math.atan2(_ymouse-player._y,_xmouse-player._x)
bullet.onEnterFrame=function(){
this._x+=this.rads*this.speed
if(this._x>550 || this._x<0 || this._y<0 || this._y>400){
this.removeMovieClip()
}
}
}

This will shoot a bullet from the player in the direction of the mouse every time the player presses the left mouse button.

arobegamr
offline
arobegamr
130 posts
Nomad

My mistake, but I forgot one snippet of code:
enter this at the end of the main stage actions panel:
Mouse.addListener(mouseListener)

arobegamr
offline
arobegamr
130 posts
Nomad

Here is what you're code should look like:

bulletNum=0
var player:MovieClip = _root.attachMovie(&quotlayer",&quotlayer",1)
player._x=550/2
player._y=400/2
var mouseListener:Object = new Object()
mouseListener.onMouseDown=function(){
var bullet:MovieClip = _root.attachMovie("bullet","bullet"+bulletNum,100+bulletNum)
bulletNum++
bullet._x=player._x
bullet._y=player._y
bullet.speed=5
bullet.rads=Math.atan2(_ymouse-player._y,_xmouse-player._x)
bullet.onEnterFrame=function(){
this._x+=Math.cos(this.rads)*this.speed
this._y+=Math.sin(this.rads)*this.speed
if(this._x>550 || this._x<0 || this._y<0 || this._y>400){
this.removeMovieClip()
}
}
}
Mouse.addListener(mouseListener)


I appologize for the mistakes

markzi
offline
markzi
103 posts
Nomad

Thanks arobe but I'm getting another character and another bullet, my main character moves while the other one that appears does not, and the bullet shoots from the unmovable character, if I remove the "attach movieclip" code I go back to square one. Any idea how to fix this?

markzi
offline
markzi
103 posts
Nomad

Any suggestions? I've been at it for ages but nothing seems to work

arobegamr
offline
arobegamr
130 posts
Nomad

It sounds like you started with another movie clip on the stage. You're main stage should be completely blank, with two clips in the library. If you want a moveable character, you could add this to your main stage actions, to control the player with the arrow keys.

player.onEnterFrame = function(){
if(Key.isDown(Key.UP)){
this._y--
}else if(Key.isDown(Key.DOWN)){
this._y++
}
if(Key.isDown(Key.LEFT)){
this._x--
}else if(Key.isDown(Key.RIGHT)){
this._x++
}


If that isn't your problem, than give me some more details on exactly what is happening and I'll get back to you as soon as possible

markzi
offline
markzi
103 posts
Nomad

Thanks for the reply, but the thing is, I gave the gun a linkage of player, because I want the bullet to fire from the gun, so how do I make the newly attached gun move like the gun that is inside the character?

markzi
offline
markzi
103 posts
Nomad

If I'm not explaining very well, I could send you my .fla if you want.

gaboloth
offline
gaboloth
1,612 posts
Peasant

Hmm, are you writing code in external classes or in the frames?

markzi
offline
markzi
103 posts
Nomad

In the frames

Showing 1-15 of 18