Tuesday, July 30, 2013

Step 4: Acting classes


By now, we're looking at almost a game. Now comes the hard part. Shooting.

More specifically, the hard part is making sure that your lasers dont just stay on the screen forever. This took me quite a long time, with lots of false starts and failed attempts before I solved it. So I'm not going to give you the solution right away. I'mma make you work for it.

First, we have to add a laser as another actor. You may be thinking 'shouldnt it be a sub-class of the ship?'. That's what I thought. Trust me, for now, we're better off with seperate classes.

So do the thing, add the actor class, pick your image. Greenfoot is so handy for this, but after finishing this project, I dont think I'm going to stay in this development environment. As nice as the quick adding of classes/actors/worlds is, I'd way rather have a real coding environment like Sublime Text or Eclipse.

That being said, once you've picked your image, we have to get the ship to fire the laser.

Adding a new object to the world is very easy. All you have to do is instanciate the world class and your actor class, and then add the actor to the world. What does that look like?

Actor laser;
laser = new Laser(x, y);
World world;
world = getWorld();
world.addObject(laser, x, y);


You may be asking 'what are those x's and y's? when I try to run this, it gives me errors.'. This is true. And its because I modified the constructor class of the laser class.

What?

When 'Actor laser' is called, it is declaring 'laser' to be a variable of type 'Actor'. When you call 'laser = new Laser()', you are creating a new instance of that class. In the class, when a new instance is created, it first calls the constructor.

public Laser(int x, int y)
    {
         startX = x;
         startY = y;
    }


Any class constructor in java looks like this. The class name (capitalized), followed by the parameters, followed by the commands. Now, startX and startY are whats called 'properties' of the class. They are not functions, they are just attributes. They are declared earlier, using this:

private int startX;
private int startY;


Private just means that no other class can access them directly (this is a good thing). Just like all variable declarations in java, we then tell it what to expect (int: whole numbers, 0,1,35,-20 etc) and what to call it.

So, if you can tell me what the earlier code is doing, you get a prize. If not, you get the same prize: me telling you what it means! Huray!

Here's what it is: the spaceship class is creating a new instance of the laser class, then getting the world as it stands, and adding the laser to the world. Beautiful, really.

Now, we need to set x and y. We do this before the 'actor laser' bit, by getting the current location of the ship.

int x = getX();
int y = getY();


Simple enough. The getX and getY methods are part of Greenfoot. Handy tools.

So go ahead and compile and try. What? It's only placing the lasers where your ship is?

Well, thats something at least. Here, lets quickly fix that issue.

move(5); inside the act function. I hope you beat me to it and knew to do it, cause that means youre learning.

Now the lasers should fire, and never leave the screen, but they are all firing in the same direction, regardless of your position. Thats because we need to set the rotation.

int rotation = getRotation();
// actor stuff, world stuff, add object
laser.setRotation(rotation);


This should have you firing at will.

Do'h. I forgot to bind the firing to a key press. See if you can figure it out.