ForumsProgramming ForumHappyBlocks - Collision and Score

1 1332
1,305 posts

Happy Blocks - Adding AI

Were going to start with setting up the text field and creating the variable for the score.

Open your file and first import the text package

Don't forget to include the variable containing the score.

Now scroll down to the Main function and proceed to creating the dynamic text field.

Here is the new text field, making the selectability of it false eliminates the cursor when the mouse rolls over the text. The final line updates the text to show the score.

There are several ways of score implementation and monitoring. The way we will be doing it here will include event dispatching. Still in the Main function add the listener and the associated function.

From this point we can move into Scroll down to the AI_Movement function and update it to pass the hero as an argument to the
smaller blocks.

At a glance there seems to be no noticeable difference. The only difference is this line.


Each block that is in this array will need to be prepped to receive the passed argument. Open all the block classes. Red, Blue, Green, and files. Scroll to the loop function and add the receiving argument.

public function loop(hero:Sprite):Boolean

The rules of this game will be... The hero touches the block and is the same color as the block, the block will go from being angry to being happy, and the player scores a point. Just for fun we will also make the block move faster as an additional visual confirmation that the collision has occurred to the player.

First add a boolean that indicates whether or not the block is happy.

It starts as false due to the block is initially not happy. Now for the collision between the blocks and the hero. The smaller blocks will first check to see if it is already happy or not. This will save an additional check from the blocks if it is not necessary. You don't need to check the collision to make the block happy if the block is already happy. If the block is not happy, proceed to check if there is a collision with the hero, then if there is a collision make a final check to see if the hero is the correct color.

Once all of these conditions have been met, the block will be happy, smiling, moving faster and the player will have scored a point.


import flash.text.*;
  • 1 Reply
Showing 61-60 of 1