This site requires JavaScript, please enable it in your browser!
Greenfoot back
Andrew.2
Andrew.2 wrote ...

2019/1/28

Classes, subclasses and coords. can you help out a beginner?

Andrew.2 Andrew.2

2019/1/28

#
So i'm trying to make enemy tanks, where each enemy tank is made up of a few layers. My idea was to make a class dedicated to the coords of the enemy and a subclass for the layers. How would i let my subclass know the coords of the enemy when there are multiple enemies in the world? I tried using the subclass inheritance-creating public variables for x&y in the main class and then using them in the subclass but the layers just move to 0,0. How can i make this work?
danpost danpost

2019/1/28

#
Andrew.2 wrote...
So i'm trying to make enemy tanks, where each enemy tank is made up of a few layers. My idea was to make a class dedicated to the coords of the enemy and a subclass for the layers. How would i let my subclass know the coords of the enemy when there are multiple enemies in the world? I tried using the subclass inheritance-creating public variables for x&y in the main class and then using them in the subclass but the layers just move to 0,0. How can i make this work?
For a better idea of what you are trying to say, you should show an example of the classes you are trying to use for layers and coords.
Andrew.2 Andrew.2

2019/1/28

#
Ok. please ignore the messy/beginner code. I tried all kinds of combinations to make it work.
public class enemyspot extends Actor
{
    
    public boolean w=false;
    
     int xx;
     int xy;
    public void act() 
    { 
         
        
        
        
        xx=getX();
        xy=getY();
        if(w==false){
        for (int i=0;i<=4;i++){enemy enemy=new enemy(i);  
            getWorld().addObject(enemy,xx,xy);  w=true; }
        
        
        }
    }   
    
}
And here's the subclass
public class enemy extends enemyspot
{
    int projectilerot;
    int shotdelay=0;
    int partcopy;
    int yoffset;
    int doturn=0;
    
   
    
    public enemy()
    {
    }
    public enemy (int part)
    {
        partcopy=part;
        
        
        
    }
    
    
    
    public void act() 
    {
        
        mouse pl = (mouse) getWorld().getObjects(mouse.class).get(0);
        setImage(pl.images[partcopy+7]);
        yoffset=partcopy*3;
        stayonpoint();
        trackplayer();
       
        
    }    
    public void stayonpoint()
    {
        
        setLocation(xx,xy-yoffset);
   
    }
}
danpost danpost

2019/1/28

#
One main issue is that an enemy object is NOT an enemyspot object. So, the enemy class should extend the Actor class, not the enemyspot class. You may want to override the addedToWorld method in your enemy class to set xx and xy to getX() and getY(), respectively.
Andrew.2 Andrew.2

2019/1/28

#
You'll have to go slow with me:)). Yeah what you are saying works, but i want the enemy tanks to move,randomly if possible, so they have to move as a whole, not each layer going in a random direction.
danpost danpost

2019/1/28

#
Is each enemyspot object allowed only to create one set of tank parts?
Andrew.2 Andrew.2

2019/1/28

#
yes each enemy spot creates the 5 layers of a tank and it's supposed to be the exact location of a tank. each layer has it's enemyspot's location and y-offset for a 3d effect.
Andrew.2 Andrew.2

2019/1/28

#
i just thought of a janky solution: creating 2 x and y arrays in a different actor and each enemyspot gets a place in each array, and passes the indicator to it's layers. then every tick puts it's coords in the array and the layers can get the correct coords for their enemyspot. if there's no clear and easy way to do this i'll try it.
danpost danpost

2019/1/28

#
Andrew.2 wrote...
yes each enemy spot creates the 5 layers of a tank and it's supposed to be the exact location of a tank. each layer has it's enemyspot's location and y-offset for a 3d effect.
Then you probably would want to use an inner class for the parts of the tank. An example might be as follows:
import greenfoot.*;

public class Tank extends Actor
{
    private Actor[] parts;
    
    protected void addedToWorld(World world)
    {
        parts = new Actor[5];
        for (int i=0; i<5; i++)
        {
            parts[i] = new Part(i);
            world.addObject(parts[i], getX(), getY());
        }
    }
    
    public void act()
    {
        for (int i=0; i<5; i++) parts[i].setLocation(getX(), getY()-3*i);
    }
    
    public class Part extends Actor
    {
        private int partNum;
        
        public Part(int id)
        [
            partNum = id;
            setImage((mouse)Tank.this.getWorld().getObjects(mouse.class).get(0)).images[id+7]);
        }
    }
}
I think I did this right. Notice that the Part objects can reference the tank that created them using Tank.this.
Andrew.2 Andrew.2

2019/1/28

#
Thanks! Or should i say tanks! Ok bad joke aside this seems to work and most importantly i learned some useful things from this. I'll let you know if i have any other issues.
You need to login to post a reply.