Java - Calling another class - Something's wrong.

Soldato
Joined
14 Jul 2005
Posts
17,616
Location
Bristol
I'm creating a game for an assignment for uni which is meant to run on a mobile phone. This means I have to use netbeans with MIDlets and Wireless ToolKit etc. - That's fine.

I have created a simple asteroids style game, but I need to implement levels into it, now I figure the easiest way is to create multiple classes, one per level to stop making a big mess of the main class.

This is proving problematic, as far as I can tell, I've set up the new class to extend the previous one (will put a menu on 1st class, then levels on the following ones).
So when 'MyGameCanvas' gets to a certain point it calls up LevelTwo.

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GameCanvas;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.Control.*;


/**
 *
 * @author rob
 */

public class LevelTwo extends MyGameCanvas implements Runnable
{
    public LevelTwo()
    {
    }
    
    public void start()
    {
        
    }
    
    
}

That's all I have in the second class for now as I want to get it able to be used before I start inserting more code.

The error I'm getting from Netbeans is;

Netbeans said:
cannont find symbol
symbol : constructor MyGameCanvas(boolean)
location: class GameCanvas.MyGameCanvas

I'm sure it's something simple that I've missed out. It appears to be I'm missing something from 'MyGameCanvas', but I don't know what. I've set up the call for 'LevelTwo' in 'MyGameCanvas' and I'm sure when I've done things like this before it's been fine...although I don't normally use Netbeans (but it having phone emulators etc. means I have to use it really).

Any help greatly appreciated.

InvG
 
Are you sure there are no compile time errors being reported by NetBeans?

You should be getting an error that MyGameCanvas implements Runnable but does not implement the run() method.

Maybe you meant run() rather than start()?
 
Before i start i am a C# dev so all this may not be 100% correct, just looking at the pure OO of it all.

Firstly if its just a level why are you extending the main game class and with that in mind does the main game class have an empty constructor? if not you need call the base class constructor form the derived class the constructor's parameters.

Secondly implementing runnable looks suspects if the base class is the main thread and implements runnable you shouldn't need to re implement for that class
 
Here's a screenie of where the error is...makes it easier to explain...




I don't get why it's complaining about MyGameCanvas not haveing a MyGameCancas(); constructor. - This is mostly due to my mind blanking out what a constructor is. :p

InvG
 
Firstly if its just a level why are you extending the main game class and with that in mind does the main game class have an empty constructor? if not you need call the base class constructor form the derived class the constructor's parameters.

I would keep it all in the main class, but I've run into the fact it's a huge mess of code. I prefer to keep it tidy, plus I originally set up the game to be one level, then I read the spec and it has to be more than one. So I have to create loads more code, most of which is a near direct copy of the current stuff.

EDIT: My levels are different 'asteroids' flying around the screen. I admit I can make it so that it's a lumpymess of code, but I'd prefer to have cleaner code. I'm also toying with the idea of creating a front screen, which'll need it's own class in any case, so I still need to sort my class calling.

InvG
 
The error says in the MyGameCanvas class you are missing a MyGameCanvas contructor.

So your missing in MyGameCanvas.java:

public MyGameCanvas()
{

}
 
The error says in the MyGameCanvas class you are missing a MyGameCanvas contructor.

So your missing in MyGameCanvas.java:

public MyGameCanvas()
{

}

But I have one...

Code:
    public MyGameCanvas(Display d)
    {
        super(true);
    	myDisplay = d;
        frameRate = 40;
        zimXVelocity = 0;
        zimYVelocity = 0;
        mooses = new Sprite[numberOfMooses];
        miniMooses = new Sprite[numberOfMooses];
        shield = new Sprite[maxShieldLevel];
        secondLevel = new LevelTwo();
    }

:confused:

InvG
 
As I said above you need to call the base constructor with parameter for the base class o work. So make the constructors match or if your initing that var in there you need to call the base constructor with it
 
I'm now even more confused. :( and am being really thick this morning. :(

Code:
MyGameCanvas extends GameCanvas implements Runnable

GameCanvas contains the initial running of the game. We were given a template which contained GameCanvas as the actual runtime class, which calls on MyGameCanvas which contains the game content.

I have lots of things in the MyGameCanvas that I need to have interchangeable between the new classes (LevelTwo at the moment, but will be LevelOne, LevelThree etc.), like the scores and image loadings.

This says to me I need to create a child class of MyGameCanvas and have it called when it is needed, for example, when the first level of the game is completed. But when I write it how I figure it should be, I get that error.

This morning's thickness means I can't remember what a constructor is.

What has been said is probably fine and will work, just my head is not working so I need explanations. :p

Cheers, and sorry for being a pain.

Really shouldn't leave assignments to very late in the deadline. :(

InvG
 
Hi,

Another way of doing this would be to use classes for each of the main game objects that you can change for each level. Level-specific attributes for each of these would then be specified by attaching a "context" object to it. Finally, factory objects would be used to produce the main game objects complete with their context information.

For example, your asteroid class will define a generic lump of rock containing methods that will determine how it is drawn, it's position on screen etc. Everything that every asteroid does in order to be an asteroid.

Attached to this will be an asteroid_context object that will define the speed of the asteroid, size, number of frags it will break into etc. So, to get asteroids for different levels you'll use the same asteroid class (which contains generic behaviour) but with a different context object attached to it (denoting level specific info).

To produce the objects for each level a factory class would be used rather than creating them directly in your main game code. For example, you could have an "asteroid_factory" class. This class will have a method on that will create a new asteroid e.g getAsteroid(), and will accept a parameter to indicate the level of asteroid that you want. The factory class will create the asteroid, it's associated context object (to define the level of the asteroid) and will then return your asteroid.

In your main game class creating an Asteroid will be a simple matter of using the factory. Taking the level specific stuff out of the asteroid class (and putting it into the context) will make it easy to configure your game, add more levels etc.

Hope that helps.

Jim
 
Well because my inheritance would not work :( I just gave up and have created loads of methods and if statements that call the respective methods. Not as clean as I'd like, but it works. :)

Although it's meant to display an image before each level, and I can only get it to display 'Level 1' (which is actually an image/sprite). It just goes straight onto level 2 with no warning.

I would post all my code, but for the whole plagiarism thing due to being uni coursework I don't feel it's the best of ideas.

Cheers for the help though guys. :)

InvG
 
Not being rude but if you don't what a constructor then your attempt at OOP was doomed from the start.

The best way to do what you attempted is to create a main class to handle the running then each level to be a class than inherits from an interface that has common methods to run each level. But if a constructor is beyond your knowledge an interface or abstract will be pushing it
 
Not being rude but if you don't what a constructor then your attempt at OOP was doomed from the start.

The best way to do what you attempted is to create a main class to handle the running then each level to be a class than inherits from an interface that has common methods to run each level. But if a constructor is beyond your knowledge an interface or abstract will be pushing it

That's exactly what I was trying to do, a main class, and the levels derive from the main one, but my inheritance went to pot. :(

I'm fine with coding etc. it's just using words like 'constructor' just goes over my head because we haven't been taught it properly. Our first year lecturer was a joke when it came to OOP/Programming/Java, and the one this year assumes that the previous one did it properly and that we know everything. :(

Isn't this which is in MyGameCanvas.java a constructor?

Code:
    public MyGameCanvas(Display d)
    {
        super(true);
    	myDisplay = d;
        frameRate = 40;
        zimXVelocity = 0;
        zimYVelocity = 0;
        mooses = new Sprite[numberOfMooses];
        miniMooses = new Sprite[numberOfMooses];
        shield = new Sprite[maxShieldLevel];
    }

InvG
 
Yeah, that's the construtor, a constructor is a method with the same name as the class but no return type and it gets called when the class is created. If you inherit from a class you must pass in the parameters to the base class parameters so if

Class 1 has a constructor that takes a string then class 2 that inherits class 1 ust pass a string to the base (super constructor) for it to build
 
Ok, so in the case of mine MyGameCanvas.java is being passed the Display from InvaderZimGame.java

So if I were to make a child class from MyGameCanvas.java I need to pass....something? - Anything I put it throws a fit :p

And for it to be worthwhile in the view of OOP everything I declare and require later on should be initiated in the constructor.

For example...
Code:
    public void start()
    {
        myDisplay.setCurrent(this);
        sleeping = false;
        zimCollisionDetected = false;
        plasmaAlive = false;
        plasmaHit = false;
        count = 0;
    
        ...

    }
...those initiating objects should all be in the constructor...actually I'm not sure why I didn't put them there in the first place...


InvG
 
Hi,

Constructors are quite tricky things, at least I found them to be.

When a subclass' constructor is performed it invokes the no-args constructor on the baseclass, unless the first line in the subclass constructor is explicitly stated as super(args) .

It's usual to create an empty constructor in a class so that the subclass constructor has something to call in the case that the super(args) isn't performed as the first statement. In fact if you don't declare the no-args constructor you'll get a compile error if a constructor of a childclass doesn't use the super(args) call. The no-args constructor is supplied automatically only if a class has no constructors defined for it.

I don't think you necessarily need to set everything in the constructor. It could be that some things won't be known at the time the constructor is called. However, the constructor should build the class to be in a valid state. Once the main game object has been created other things can be set using getters/setters.

In the asteroids game I would define one class to handle the main game (movement, scoring etc) and a separate class to provide/define each level's parameters, each of these implementing an interface. That way the level code is kept separate from the actual game code thus ensuring that each level uses the same main game code but without needing inheritance etc. It also makes it much easier to change where the level info is read from e.g XML file etc. and it's format.

Just one way of doing things, lots of other ways it could be done too.

Jim
 
Back
Top Bottom