Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Event press space to fire bullet stops continuous move with pressing arrow
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
Hi everyone,
I'm creating a very simple game where a spacecraft, low on the screen, moves left and right and shoots some enemy who is hovering on the top half of the screen.
I have created two events for moving and when arrows left and right are pressed, the spacecraft moves left and right accordingly, making 10pxl moves.
One more event, pressing space, will create a clone of the bullet and the bullet will start moving towards the top of the screen.
Now, these events don't interact the way I want it.
When I keep press say the right arrow, the spacecraft makes a continues move (in small jumps of 10pxl) which I like, but then when space is pressed to fire a bullet, the continues move will stop and I have to lift my finger and press the arrow again.
I'd like to be able to fire bullets and keep the continuous move.
Is there a way to correct this? I hope I described it well enough.
I'm creating a very simple game where a spacecraft, low on the screen, moves left and right and shoots some enemy who is hovering on the top half of the screen.
I have created two events for moving and when arrows left and right are pressed, the spacecraft moves left and right accordingly, making 10pxl moves.
One more event, pressing space, will create a clone of the bullet and the bullet will start moving towards the top of the screen.
Now, these events don't interact the way I want it.
When I keep press say the right arrow, the spacecraft makes a continues move (in small jumps of 10pxl) which I like, but then when space is pressed to fire a bullet, the continues move will stop and I have to lift my finger and press the arrow again.
I'd like to be able to fire bullets and keep the continuous move.
Is there a way to correct this? I hope I described it well enough.
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Sharing what you have makes it easier to help.
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
ok, I just shared it. Can you visit it?
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Don't use the keypress hat blocks, use a forever loop to detect the left and right key presses.
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
I see. I modified the code. Is that closer to your suggestion?
Only then, the bullets were firing too close one after another and I modified my code even further and tried to solve this as follows:
there is a variable “shoot_next” that is either:
• zero: not allowed to shoot next bullet
• one: you may shoot next bullet
(btw aren't there other variable types like boolean, text etc?)
when a bullet clone starts, it sets this variable to zero (disallowing a new shot for a while) and triggers a message titled “just_shot”. This message is received by the spacecraft and the idea is that while the bullet goes its way, there will be a delay of say, one second, and thereafter variable “shoot_next” becomes 1, allowing a following up bullet. Meaning that firing a bullet now needs both pressing space and having shoot_next set to 1.
But.
I doesn't work. The bullet appears and waits there all that delay time before starting going up. But I can't seem to see why, when I check my code.
What is the problem here?
Only then, the bullets were firing too close one after another and I modified my code even further and tried to solve this as follows:
there is a variable “shoot_next” that is either:
• zero: not allowed to shoot next bullet
• one: you may shoot next bullet
(btw aren't there other variable types like boolean, text etc?)
when a bullet clone starts, it sets this variable to zero (disallowing a new shot for a while) and triggers a message titled “just_shot”. This message is received by the spacecraft and the idea is that while the bullet goes its way, there will be a delay of say, one second, and thereafter variable “shoot_next” becomes 1, allowing a following up bullet. Meaning that firing a bullet now needs both pressing space and having shoot_next set to 1.
But.
I doesn't work. The bullet appears and waits there all that delay time before starting going up. But I can't seem to see why, when I check my code.
What is the problem here?
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Because you broadcast and wait and the spacecraft has a wait 1 secs so the loop to move the bullet doesn't start until the broadcast receiver has finished.
Use a broadcast instead of a broadcast and wait.
Use a broadcast instead of a broadcast and wait.
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
ah, *! I didn't notice that picked the wrong block! And I thought, what the heck!
thanks deck26![](//cdn.scratch.mit.edu/scratchr2/static/__868b8818d76143ec272057b47660c443__/djangobb_forum/img/smilies/smile.png)
thanks deck26
![](http://cdn.scratch.mit.edu/scratchr2/static/__868b8818d76143ec272057b47660c443__/djangobb_forum/img/smilies/smile.png)
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
I moved on. It’s fun. Nevertheless, I can’t seem to grasp a couple of things.
a) I put a fire sound when a bullet clone starts. It plays but with a lot of lag. Why is that?
b) when hit and before destroying itself, the enemy clone creates a new clone of itself. The idea is that it gets replaced by a new enemy. Instead, two clones appear for every hit. I have inspected the code but I can’t seem to see why.
One solution is to move the create enemy clone to the spacecraft sprite. Then it works alright. But I need to know, why the heck do two clones appear when the code it is like I got it now?
thanks
a) I put a fire sound when a bullet clone starts. It plays but with a lot of lag. Why is that?
b) when hit and before destroying itself, the enemy clone creates a new clone of itself. The idea is that it gets replaced by a new enemy. Instead, two clones appear for every hit. I have inspected the code but I can’t seem to see why.
One solution is to move the create enemy clone to the spacecraft sprite. Then it works alright. But I need to know, why the heck do two clones appear when the code it is like I got it now?
thanks
Last edited by Scrunner (Dec. 15, 2017 18:26:51)
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
How do you see the lag - it might be faster but wasn't too bad for me.
I'm baffled at the moment regarding the clone behaviour - the number of clones does seem to be out of control. However you should let the enemy clones detect being hit by the bullet rather than vice versa as otherwise each enemy clone gets the broadcast when one is hit. That seems to fix the problem as well - just use a separate ‘when I start as clone’ script with ‘wait until touching bullet’. So not an explanation but a better way of doing it anyway.
I'm baffled at the moment regarding the clone behaviour - the number of clones does seem to be out of control. However you should let the enemy clones detect being hit by the bullet rather than vice versa as otherwise each enemy clone gets the broadcast when one is hit. That seems to fix the problem as well - just use a separate ‘when I start as clone’ script with ‘wait until touching bullet’. So not an explanation but a better way of doing it anyway.
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
Funny. I was running the online editor in Firefox and there the laser sound had some noticeable lag. I tried it with Slimjet and Chrome, and there it works just fine. (btw I avoid using Slimjet because it always hangs eventually and I lose my progress).
Last edited by Scrunner (Dec. 16, 2017 00:22:44)
- MacaroniMailbox
-
100+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Yeah what he said. It seems that not only are these Don't use the keypress hat blocks, use a forever loop to detect the left and right key presses.
when [ v] key pressedunable to detect multiple key presses at once, movement is much blockier and less smooth.
- PutneyCat
-
500+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Cured already so doesn't really matter but I think the issue with clone numbers was because the original enemy sprite (as opposed to any clones of it) always remains even if hidden. So it (as well as the clones) was responding to the “enemy kill” broadcast. “Delete this clone” as used in the green flag script doesn't, I think, have any effect on the original sprite.
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Can't believe I overlooked that - it's a very common mistake. Cured already so doesn't really matter but I think the issue with clone numbers was because the original enemy sprite (as opposed to any clones of it) always remains even if hidden. So it (as well as the clones) was responding to the “enemy kill” broadcast. “Delete this clone” as used in the green flag script doesn't, I think, have any effect on the original sprite.
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
I thought about it but didn't check it further. Indeed, that was the problem.
Do check my “testing01” project. When pressing “space” a message is sent an recieved by the enemy clone and makes it “hum”. But then, the original sprite is humming along, too.
So, what is the way out of it? I mean, this is programming and there are lots of solutions, but what is customary?
In my other project, I solve it by moving the enemy clone creation to the spacecraft. But I’d much prefer the enemy to do its own administration.
thnx
Do check my “testing01” project. When pressing “space” a message is sent an recieved by the enemy clone and makes it “hum”. But then, the original sprite is humming along, too.
So, what is the way out of it? I mean, this is programming and there are lots of solutions, but what is customary?
In my other project, I solve it by moving the enemy clone creation to the spacecraft. But I’d much prefer the enemy to do its own administration.
thnx
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Use cloneID - set this as a local variable and set it to 0 for the sprite and 1 for the clone(s) and you can tell them apart.
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
I was just experimenting.
CloneID is one single variable accesible by all clones. When it's updated, all clones get to see that new value. So how can this help to tell the clones appart?
CloneID is one single variable accesible by all clones. When it's updated, all clones get to see that new value. So how can this help to tell the clones appart?
- deck26
-
1000+ posts
Event press space to fire bullet stops continuous move with pressing arrow
Not if it's a local variable - this sprite only. Set it to 0 for the sprite and at the top of the ‘when I start as clone’ script set it to 1. If you monitor the sprite's copy you'll see it doesn't change but try getting a clone to say what the value is and it will say 1. I was just experimenting.
CloneID is one single variable accesible by all clones. When it's updated, all clones get to see that new value. So how can this help to tell the clones appart?
- Scrunner
-
11 posts
Event press space to fire bullet stops continuous move with pressing arrow
dαmn! I would swear I had set it as local. Anyway, now it's right. Thanks! ![](//cdn.scratch.mit.edu/scratchr2/static/__dd00aa92a78a279a4faed9a4a61d15fa__/djangobb_forum/img/smilies/smile.png)
![](http://cdn.scratch.mit.edu/scratchr2/static/__dd00aa92a78a279a4faed9a4a61d15fa__/djangobb_forum/img/smilies/smile.png)
Last edited by Scrunner (Dec. 18, 2017 14:03:31)
- Discussion Forums
- » Help with Scripts
-
» Event press space to fire bullet stops continuous move with pressing arrow