How do I make a character do rapid fire
Monkey Forums/Monkey Beginners/How do I make a character do rapid fire
| ||
I was wondering what is the best way to make a character in a game do rapid fire in the right direction if the character is facing rightward and the left direction if the character is facing the leftward direction. BTW, I wanted to do this using a png image for the bullet. Thanks! |
| ||
I control the rate of fire with a simple gun timer and only allow a shot to be fired if the timer has expired. psudocode. Global GunTimer:Int = Millisecs() Global GunCoolDown:Int = 500 ' 1 bullet every half a second. decreasing will increase the rate of fire. Method UpdateTimers:void() if Millisecs() > GunTimer+GunCoolDown GunTimer=Millisecs() 'OK TO FIRE A BULLET endif End Method As for the direction your thing is facing just store it as a field of the class its part of , something like Field facingDirection:Int and if you press Left set that field to something like 1, and if you press right set it to 2, then when your shooting just check the direction your facing and shoot the bullet in the appropriate direction. |
| ||
Here is something you can actually run. |
| ||
Paul, thanks for the code. I tried to run the code. I copied and pasted the it into Monkey, but the bullets are not showing when I hit the space bar or when I use the arrow keys to change directions of the ship. I tried to adjust it myself but I don't know what I'm doing wrong. |
| ||
I just tried the code and it works fine for me. What exactly are you getting in your browser window (I assume you're building for HTML5). Maybe double check you copied the entire code that Paul posted? |
| ||
Ok, I tried it again but I only got the ship with no bullets. Maybe I'll work with it a little more. I tried it from the desktop and browser. |
| ||
it should all work fine, does it provide you with any errors? does the ship move if you hit left or right arrows? I suspect your just not getting a full copy, failing that try a different browser, because the above code is fine. |
| ||
I actually didn't get an error. The ship indeed appeared. It's just that when I press the right and left arrow keys, the ship doesn't change directions or shoot bullets. But, I think if it's working for everybody else, then maybe I'm doing something wrong. I just have yet to detect it. I copied and pasted the code in as:Strict Import mojo Function Main:Int() Local ExampleApp:= New ExampleApp Return 0 End Function Const LEFT:Int = 1 Const RIGHT:Int = 2 Class ExampleApp Extends App Field gunTimer:Int Field gunDelay:Int Field playerX:Int Field playerY:Int Field playerDirection:Int Field shooting:Bool Method New() Self.gunDelay=100 'change this to change the rate of fire. Self.playerX=640/2 Self.playerY=480/2 Self.playerDirection = LEFT Self.shooting=False End Method Method OnUpdate:Int() If KeyDown(KEY_LEFT) Then Self.playerX-=2 Self.playerDirection=LEFT Endif If KeyDown(KEY_RIGHT) Then Self.playerX+=2 Self.playerDirection=RIGHT Endif If KeyDown(KEY_SPACE) Then Self.shooting=True Else Self.shooting=False Endif If Millisecs() > Self.gunTimer+Self.gunDelay And Self.shooting Self.gunTimer=Millisecs() Local NewBullet:cBullet = New cBullet(Self.playerX,Self.playerY,Self.playerDirection) Endif For Local B:cBullet = Eachin BulletList B.Update() Next Return 0 End Method Method OnRender:Int() Cls DrawRect(Self.playerX,Self.playerY,30,30) Select Self.playerDirection Case LEFT DrawRect(Self.playerX-10,Self.playerY+10,10,10) Case RIGHT DrawRect(Self.playerX+30,Self.playerY+10,10,10) End Select For Local B:cBullet = Eachin BulletList B.Render() Next Return 0 End Method End Class Global BulletList:List<cBullet> = New List<cBullet> Class cBullet Field x:Int Field y:Int Field speed:Float Method New(_x:Int , _y:Int , _direction:Int) Self.x=_x Self.y=_y Select _direction Case LEFT Self.speed=-5 Case RIGHT Self.speed=5 End Select BulletList.AddLast(Self) End Method Method Update:Void() If Self.x<0 Then BulletList.Remove(Self) Self.x+=Self.speed End Method Method Render:Void() DrawRect(Self.x,Self.y,10,10) End Method End Class I also tried running it from the desktop, different browsers, etc. I just might need moment to figure out why it hasn't worked, because again maybe I'm doing something wrong: |
| ||
you can throw some Print "Something" around the code and try and see where things are going wrong, if the thing is not shooting or moving then I would start by throwing one inside each of the If KeyDown calls to make sure the key press is being detected, after that I would check that a new bullet is actually being made and added to the list, throw a print into the bullets New method. let us know how you get on, only thing I can think of is delete your .build folder and rebuild, or its something to do with the version of monkey your using.. the code looks fine tho. |
| ||
Ok thanks Paul. I'll keep working with the code and will give an update. BTW, I'm using the trial version of Monkey. I don't know if that has an influence or not, but I'll try again. |
| ||
The trial version of Monkey will not be an issue, it is a full version without being able to compile to all targets. Have you tried running any of the Monkey games that are in the Projects or Apps sub-forums - long shot, this will just make sure nothing funky is going on with your Java/Browser. |
| ||
Abrabia, I actually wrote whole games in Monkey-X both for browser and desktop (almost for 2 years now) and I tried the projects in the folder and they all work and now I've tried it in 3 different browsers from the most updated versions of Internet Explorer, to Google Chrome, to Fire Fox. I've also tried deleting the build folder and starting over. I've copied and pasted stuff from this forum quite a few times. Thus, I don't know why the format is suddenly not working correctly. Again, I see the ship when I run the example above (exactly as written), but there's no bullets being fired. I'm thankful for all the help. |