PDA

View Full Version : [AS3] Block Breaker Game Help


Virtual_Skippy
07-15-2009, 02:48 PM
Hi there everyone, hope your all well. Every time I Googled a problem on AS3 I always ended up arriving at this great site so I finally decided to register. I also have a slight issue with a Block Breaker game I am currently trying to build.

A bit of background: I'm currently reading "Macromedia Flash Professional 8 Game Development" by Glen Rhodes and attempting to update the games from AS2 to AS3. Only differences I have are stage sizes and array sizes from what is discussed in the book as eventually I am hoping to host the games on my PHP website dynamically. I'm currently working on a block breaker game found in the book and cannot for the life of me figure out how to detect a collison between a movie clip and an array of movie clips that builds the 'Block Grid'. I hope I explained that clearly enough :).

Multiple errors have surfaced while trying to resolve this isse, ranging from the child references being set to null on the odd occasions I can remove movie clips right through to not being able to convert Strings to MovieClips.

Attachted to this post is a copy of the game that is configured up to the point where no collisions are detected between my ball movieclip and the grid. May I ask kindly that my fellow actionscript programmers take a look at my project and attempt to get it to the point where a collison is being detected between the movie clips in the array and the ball movie clip?

Many days have been spent on this just sat at my computer racking my brains with this. Any help would be supremely appreciated.

Thank u :)
Skippy

lemoncurds
07-15-2009, 07:21 PM
Ok here you go this should work

lemoncurds
07-15-2009, 07:25 PM
Lemme explain what I did

In the buildBoard function i pushed all the blocks into the mBlockArray
Then I made a new loop in movePaddle( I couldn't be bothered to make a new function, you can if you want). It loops through all the blocks to see if they are hitting the ball, and if they are removes it and also removes that block from the array so the next time round it doesn't check if it's hitting something that doesn't exist.

I also moved the addEventListener for movePaddle to the initsetup so that the loop wouldn't check if it was hitting before the blocks had been added to the stage

Virtual_Skippy
07-16-2009, 11:06 AM
@Lemoncurds
thats absolutely brilliant. I'd been struggling with creating 2d arrays and loading Objects into them. That seems to have been the problem. Thank you so much, u have calmed 3 days worth of frustration. I owe u one.

One thing I noticed thouh is that when starting a new game collisons are detected but some of the blocks do not disppaer. Could this have something to do with the hitTest (I'm not normally a big fan of the hitTest)?

Thanks again
Skippy

lemoncurds
07-16-2009, 02:20 PM
oops my bad, I never actually started a new game so I didn't notice.
The problem is when you click the begin button and it calls initSetup() and then buildBoard() it adds all the new blocks on top of the old ones and pushes them into an array thats already got blocks in it. You need to get rid of the blocks and wipe the array. So I changed your initSetup() to


function initSetup(MouseEvent:Event) {
for (var j:int = 0; j<mBlockArray.length; j++) {
for (var i:int = 0; i<mBlockArray[j].length; i++) {

mBlockArray[j][i].parent.removeChild(mBlockArray[j][i]);

}
}
mBlockArray.splice(0,mBlockArray.length);
mLives=5;
buildBoard();
mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.addEventListener(Event.ENTER_FRAME, ballMove);
}

I added a loop similar to the one for collisions, but I got rid of the hitTest obviously and simply remove all the blocks. I then completely empty the array and after that I reset the lives cos the negative ones were bugging me :rolleyes:

Hope that helps

Virtual_Skippy
07-16-2009, 04:52 PM
ah u put it in the initSetup(). I was messing about with similar code placing it in every function apart from the initial one. Awesome. It all works super fine now. I am truly grateful, u have broadened my understanding and uses of arrays and for loops no bounds.

Of course you have also helped me to create a nice little game to play too. Can I add credit to yourself to the game once it has been published? Either as lemoncurds or if u have another name I will happily place credit where credit is due :).

Many thanks once again
Skippy

lemoncurds
07-16-2009, 06:39 PM
lol, no need to give me any credit, your the one that put in all the effort to make the rest of the game after all, and after 3 days of trying still hadn't given up like I'm sure a lot of other people would have done. I'm just happy to help in any way I can :)