03-27-2007, 10:34 PM
|
#1
|
|
Registered User
Join Date: Aug 2005
Posts: 87
|
Shooting Problem...
I am having some trouble with the shooting aspect in my side-scrolling game.
So far, I've made the bullet invisible, and when a key is pressed it becomes visible and starts moving in the proper direction. The problem is, once I let go of the key, the bullet just stands still in mid-flight. I've tried adding some code and getting rid of some others, but does anyone know a way that I can achieve this?
P.S. If my explaining of the problem or what I want the solution to be is unclear, tell me and I'll try to tell you in a different way what I mean.
Thanks in advance - JohnnyTightLips
|
|
|
03-31-2007, 08:54 PM
|
#2
|
|
deaf guy
Join Date: Jul 2006
Posts: 183
|
My guess is that you're using isDown to detect whether the key is pressed, and if so, moving the bullet:
ActionScript Code:
if(/*the key is pressed*/){
bullet._visible = true;
bullet._x += speed;
}
or some such thing. The trouble then is that it only moves the bullet if the key is down. As soon as you release the key, the bullet stops. Instead you should probably use something more like this:
ActionScript Code:
if(/*the key is pressed*/){
bullet._visible = true;
bullet.onEnterFrame = function(){
this._x += speed;
}
}
That way it sets the bullet to move every frame when the key gets pressed.
__________________
The only way to debug a program is by the Scientific Method
1. Define the problem (What is your bug?).
2. Collect information (Make sure all functions do what you think, watch variables).
3. Form a hypothesis (Propose a cause of the bug).
4. Test the hypothesis (Run the program with that factor different).
5. Make a conclusion (Figure out what's happening and how to fix it).
Don't bother communicating the results.
The text has been frozen!!!
|
|
|
04-07-2007, 12:37 AM
|
#3
|
|
Registered User
Join Date: Aug 2005
Posts: 87
|
Thanks so much Def Gie, that helped me a lot. But now I seem to be having other problems that I cannot solve.
1) When I shoot and let go of the shooting button, the bullet now moves in the proper direction (thanks to Def), but when I use my other-direction-shooting button, the same bullet turns around and goes the other way. Is there a way I can say: "When you let go of this key, you have no control of the bullet untill it dissapears"?
2) I tried to set my code so that when the bullet hits a certain x-coordinate, that the bullet dissapears, but then when I test my game, the bullet still passes through and doesnt't dissapear. What is the proper code I should use for a situation like this?
Thanks in advance for any help - JohnnyTightLips
|
|
|
04-07-2007, 06:02 PM
|
#4
|
|
deaf guy
Join Date: Jul 2006
Posts: 183
|
1) How about something such as,
ActionScript Code:
if(/*the key is pressed*/ && bullet._x == /*the starting _x of the bullet*/){
/*Set the bullet to move as before*/
}
so that it only sets the bullet to move if it's where it starts.
2)I can only guess at this one, but I have a few ideas. I think you should try using >= to check if it's at or past a certain coordinate, rather than == to check if it's at the coordinate. Sometimes incremented variables unexpectedly skip values you want them to hit (usually because they're not whole numbers to begin with). Then you'll want to make it invisible again and restore its original coordinates, right? Be sure to also set
bullet.onEnterFrame = null;
or
delete bullet.onEnterFrame;
in the process or it will keep moving after you hide and return it.
As usual, I hope that helps.
__________________
The only way to debug a program is by the Scientific Method
1. Define the problem (What is your bug?).
2. Collect information (Make sure all functions do what you think, watch variables).
3. Form a hypothesis (Propose a cause of the bug).
4. Test the hypothesis (Run the program with that factor different).
5. Make a conclusion (Figure out what's happening and how to fix it).
Don't bother communicating the results.
The text has been frozen!!!
|
|
|
04-07-2007, 08:19 PM
|
#5
|
|
Registered User
Join Date: Aug 2005
Posts: 87
|
Thanks again Def, I used your help as a guide and found out the problems I had, but now a new one has come up which I didn't pay attention to before. This will be hard to explain so I posted me .fla.
My bullet works fine - you can shoot the proper way, you can't control it once it's left the starting coordinate, it dissaperas once it has reached a certain point. But now I have figured out, that when you move before you shoot, the bullet remains hidden. If you start my game by shooting, and then move to a new spot before the bullet dissapears, you will be able to shoot again. However, if you move after the bullet has dissapeared, or hasen't appeared at all for that matter, the bullet will remain hidden.
Does anyone know what the problem is?
Thanks in adavnce for any help - JohnnyTightLips
P.S. the .fla format is for Flash MX 2004, so if it won't run on your computer, send reply about this problem, and I will send another .fla in the format that will work for you.
|
|
|
04-07-2007, 08:20 PM
|
#6
|
|
Registered User
Join Date: Aug 2005
Posts: 87
|
Here's the .fla. I forgot to put it on the other reply  .
|
|
|
04-09-2007, 02:54 PM
|
#7
|
|
deaf guy
Join Date: Jul 2006
Posts: 183
|
Oh, yet again. The bullet gets placed at the current position of the man + 1, and stays there, besides when it is fired. Therefore the position test does not return true. If you test for its being invisible instead of in the right position (maybe I should have thought of this in the first place), and then set it to the right position, then it shouldn't have that problem.
Instead of
ActionScript Code:
if (Key.isDown(65) && (_root.bullet._x ==_root.man._x +1)) {
in the manner I first suggested
something more like
ActionScript Code:
if (Key.isDown(65) && (_root.bullet._visible == false)) {
_root.bullet._x = _root.bullet._x + 1
//other stuff
Thinking ahead a little, with this system you might have situations where enemies (or whatever you shoot) get hit by the invisible bullet. You should probably give it an underground _y coordinate while it's inactive and move it to the right _y when it gets fired.
__________________
The only way to debug a program is by the Scientific Method
1. Define the problem (What is your bug?).
2. Collect information (Make sure all functions do what you think, watch variables).
3. Form a hypothesis (Propose a cause of the bug).
4. Test the hypothesis (Run the program with that factor different).
5. Make a conclusion (Figure out what's happening and how to fix it).
Don't bother communicating the results.
The text has been frozen!!!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 11:59 PM.
///
|
|