2

In my game, clones of bats appear and move slowly down the screen and are to be shot down. The biggest problem that I'm having is that for some reason it isn't always registering when the laser bolt and bat actually touch. It will eventually register and the bat will disappear, but not consistently. The bats should be shot down with one hit, but if you play the game you'll notice the score (one point per hit) shoots up dramatically faster than the number of bats spawning. In other words, the laser bolt seems to always sense that it's touching the bat but not the other way around.

A secondary issue I have is that the original spirit appears at the start of the game when it's supposed to be hidden. I'm stumped as to why it keeps appearing.

Here's a link to the game: http://scratch.mit.edu/projects/25310626/#player

2

What you are seeing is subtle problem called a 'race condition' which is encountered quite often in programming, especially in asynchronous programs like yours.

At the moment your program is designed so that bolt will remove itself when it touches a bat. The bat also removes itself when it touches the bolt. This should work in theory, but it requires that both the bat and the bolt are checking that condition at the same time. However there is nothing tying the program of the bolt to the program of the bat, and so the programs will almost always be out of sync.

In other words the bat program will not be checking the hit with the bolt at exactly the same time that the bolt is also checking the hit with the bat. The result is that either the bat or the bolt will detect the hit first and remove itself from the game before the other has a chance to check for a hit.

More specifically, if the bat detects the hit with the bolt, it will remove itself from the screen. When the bolt comes to checking if its hitting anything, the bat would have already been removed, and so the bolt will not detect the collision.

Consider what happens if the bolt program runs before the bat program. Assuming the bolt has just hit the bat, the program for the bolt will do something like this:

If touching bat then
Change score by 1
Wait 0.1 secs
Delete this clone

Now the program for the bat runs:

If touching bolt then
Hide
Delete this clone

However the bolt just deleted itself prior to this, and so the bat will not detect that it was touching the bolt, resulting in the behaviour you're seeing - the bolt detects the collision and disappears, but this all happens before the bat gets a chance to see that it collided with something, and so it carries on its merry way.

A better way to handle this scenario is to use an event. Events are the brown pieces in Scratch.

By using an event you can bind the behaviour of the bat to the bolt, so that the bat checks for a collision at the same time that the bolt does. To create an event you need a broadcaster (the bolt) and a receiver (the bats). When the bolt detects a hit it broadcasts an event, something like "collide". The bat receives the "collide" event, and removes itself if it's colliding with the bolt.

To answer your second question - just delete the bat off the stage, then go to the edit menu and select undelete. This should result in the bat staying in the asset area, without having to start at a fixed position on the stage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .