Time for action — creating bullet behaviors

Now that we have created the image for our bullets, we have to take it into GameSalad and start creating the bullet behaviors. This will involve movement, spawning, and more; so, let's get right into it.

  1. Anyways, let's import that image into GameSalad. Create an actor and call it Bullet and drag in the bullet image (or directly drag the image from the Library to the Actors section to create an actor from that image). Double-click that actor so we can edit it. Under the Graphics roll-out, change the Blending Mode to Additive. Now, drag in a Move behavior and change the Direction to 90, and the Speed to 1000.
    Time for action — creating bullet behaviors
  2. Next, go back into the scene editor, so we can edit our Player actor. Double-click it and under the Keyboard Movement group, create a new rule, name it Shooting and change the settings to Actor receives event | key | space | is | down.
  3. Now, simply add in a Timer behavior, and change it to Every 0.2 seconds. Then, drag in two Spawn Actor behaviors. Change the settings of the first one to:
    • Actor:Bullet
    • Direction:self.Rotation
    • Position: -8 : 5
    • Layer Order:in back of actor
    • Direction Relative to:scene
  4. Change the settings of the second one to:
    • Actor:Bullet
    • Direction:self.Rotation
    • Position: 8 : 5
    • Layer Order:in back of actor
    • Direction Relative to:scene
  5. If you're confused, take a look at the following screenshot to see if you did everything correctly.
    Time for action — creating bullet behaviors
  6. What this does is spawns a bullet at each of the barrels. Now, we are going to add in some cool-looking muzzle flashes.
    Time for action — creating bullet behaviors
  7. This was done in a 3D program, but don't worry about the black outline; once we import it into GameSalad, we will use the blending mode to remove that.
  8. Import it into GameSalad, then create a new actor and name it Muzzle Flash. Double-click it so we can edit it. Change the Blending Mode to Additive. Drag in a Timer behavior and change the timer to 0.1 seconds, then drag in a Destroy behavior.
  9. Now, go back into the Player actor, so we can add in the muzzle flashes. All we have to do is simply clone the two Spawn Actor behaviors and change the clones to Muzzle Flash, and change the second position to 50 or more, whatever makes it look like it's coming right out of the barrel.
    Time for action — creating bullet behaviors
  10. Take a look at the previous screenshot to see if everything is correct in your project. Now, let's save it and try it out.
Time for action — creating bullet behaviors

Voila! It looks pretty good, right? Now, we have to "program" the bullet collisions with the enemies.

What just happened?

In a few short moments, we were able to make our character shoot bullets in the direction our player faces and even spice it up with some muzzle flashes. A simple Spawn Actor behavior makes it quite easy to create many bullets! Unlike Xcode and programming our games, we don't have to set up an NSMutableArray similar to the following code:

(This code is here purely to show you the contrast between coding our game, and using GameSalad.)

//declare this in the header file
NSMutableArray *Array;
UIImageView *charShot;
BOOL shooting;
//code this in the main file
for(charShot in Array) {
CGPoint newCenter = charShot.center;
newCenter.x = newCenter.x + 25;
charShot.center = newCenter;
}
if (Array == nil) {
Array = [[NSMutableArray alloc] init];
}
UIImage *bulletImage = [UIImage imageNamed: @"image.png"];
charShot = [[UIImageView alloc] initWithImage: bulletImage];
if (shooting == YES) {
for(int i=0; i<1; i++) {
int x = character.center.x + 25;
int y = character.center.y;
[self.view addSubview:charShot];
[Array addObject:charShot];
}
}

That's how you code it. Now that you look at it, doesn't it make you happy that you are using GameSalad? That didn't even include the collisions, muzzle flashes positions, and autodestruction! I'm not even going to begin to explain how to program that... Ok, now let's get back to work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.67.27