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
 
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()
{

}

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
 
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
 
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

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
 
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
 
Back
Top Bottom