Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Tuesday, 4 September 2012

Select Your Side

Today I've spend about 3-4 hours working on my game, I've gotten the double jump working, bad garbage can now hit and kill rats, and the level selection page is working.

For the double jump, I was having a hard time getting it to do what I wanted, but after taking a look at the code I've been trying to use I realized something, it would be far easier so just delete the guts of the function and start over. After looking back I saw just how complicated I had been making it, when in reality I was able to cut down between 5-10 lines of code and get it to work perfectly.

When I wanted to get the garbage to his the rats as well as the rubbish I first didn't think it would have to be done any differently then the hit detection I have for the garbage hitting the rubbish or the rubbish hitting the rats, however after did some thinking I had to consider that all the garbage and rats only has two targets they were looking for, one is the single rubbish and the other is the non-moving ground. This was easy enough to do if I just kept track of the rubbish's current x and y, along with it's height and width, but since there could be multiple rats and bad garbage I had to think of a different way. I ended up making a 2D array, with each position containing a smaller array containing information about that particular bad garbage. It has it's current x and y positions, it's width and height, if it's been removed from the screen(I used a Boolean) and it's name. This allowed my rat, every time it tried to move, to scan through this array using a while loop, checking to see if it was currently colliding with any bad garbage that was still on screen. The rat kills itself then modifies the array referring to the garbage that hit it, so when the garbage updates it see's that it's collided and shouldn't be on screen anymore so it kills itself.

The level selection page wasn't that much extra work because I've been coding this game so far so it works off what the current level it, so all I have to do when a different level is selected, is to then assign stats.currentLevel to the number of the button clicked. I did have a few problems trying to catch the event that was dispatched when a level was clicked but I was able to fix this by having my map page listen for the buttons on it, then dispatch it's own event to the main page controller when something was clicked. In order to place all the different level buttons using code but without taking too much time I ended up using 2D arrays again with a while loop. Each array inside the main array just contains the x and y position of each level button, this way all I have to do is change those two numbers, which are all in one location, in order to get the buttons to move around. I'm starting to use arrays more and I have to say they really cut down on not only the lines of code that I have to write, but it also makes it easier to go back and make changes later, if you arn't a big fan of arrays I'd really suggest finding a few tutorials and trying to slowly incorporate them into your projects, before long you'll hopefully find them as helpful as I do.


Friday, 24 August 2012

SO MANY RATS

So I've made some major strides with my Rubbish game over the past few days. I had a lovely fatal error that was causing my flash program to crash whenever it encountered the problem, the really annoying part was it was randomly happening and since it was crashing flash I couldn't even read the traces to find out where the error was located.

Luckily I had time and process of elimination by my side and after commenting out large, then progressively smaller chunks of code, I was able to find where the problem was located....i can only assume. The strange part was it had to do with my garbage spawning however the problem shouldn't have been one. To decide wither the garbage that was spawned was going to be good or dangerous I had a random number chosen, 1 or 2, using my randomNumber function. Then if the number was 1 I would make my isBad Boolean false, else i would make it true, I would then pass this along to my garbage class and it would use the Boolean to choose from the correct array of items. This should mean even if for some strange reason the randomNumber function was wrong and I got a number that wasn't 1 or 2, a garbage would still be created. If I always spawned a good garbage, flash wouldn't crash, and if I always spawned a bad garbage flash wouldn't crash, I just commented out the if statements and the randomNumber generation and hard coded in the true or flash in the setUp function of my garbage.

I was finally able to fix my problem by simply having the same code, but in my garbage class so the playScreen class would just spawn a garbage and then the garbage would figure out if it was good or bad, using the same method I was trying to use on the play screen. For some reason I haven't had a crash since even though it shouldn't make a difference wither I do the randomNumber and Boolean assignment inside garbage or outside and pass it in.

I was able to get rats added, with different amounts and types spawning depending on the current level the player is on. The rats move forwards and can only be killed if the rubbish jumps and falls on them. If a rat makes it all the way to your garbage pile alive, it takes the last item you caught, along with the number of points it's worth and runs off screen. Soon I'll be able to add a few more btns to the level select screen and I'll have my basic first five levels complete.