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

2013/5/19

can someone please help?

manish1 manish1

2013/5/19

#
hello, im sorta new to greenfoot, and Im having trouble getting the API, im currently trying to program a tank that can track your movements
manish1 manish1

2013/5/19

#
i dont have anything in my tank class right now............ im totally confused
Gevater_Tod4711 Gevater_Tod4711

2013/5/19

#
What exactly do you want your tank to do?
manish1 manish1

2013/5/20

#
ok, actually now i have somthing going: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ListIterator; /** * Write a description of class greenTank here. * * @author (your name) * @version (a version number or a date) */ public class greenTank extends platformMover { /** * Act - do whatever the greenTank wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnTowardTank(); int distance = (int)(Math.random()*5)+1; move(distance); } public void turnTowardTank() { World world = getWorld(); List<Tank> tank = world.getObjects(Tank.class); ListIterator<Tank> iter = tank.listIterator(); if(iter.hasNext()) { } Tank enemy = iter.next(); int eX = enemy.getX(); int y = getY(); int x = getX(); int angle = getRotation(); if(x < eX) { if(angle == 180) { turn(); } else if(x > eX) { if(angle == 0) { turn(); } } } } public void turn() { GreenfootImage img = getImage(); img.mirrorVertically(); setImage(img); turn(180); } }
manish1 manish1

2013/5/20

#
well, i just want my tank to go after my character and shoot at it every few seconds or so
manish1 manish1

2013/5/20

#
when i compile the above code, the tank only moves forward not track my movements and come after me, it doesnt shoot either.
Gevater_Tod4711 Gevater_Tod4711

2013/5/21

#
Your turnTowardsTank method doesn't tell the tank to move to your character. Try this method:
public void turnTowardsCharacter() {
    List<Character> characters = getWorld().getObjects(Character.class);
    if (characters != null && !characters.isEmpty()) {
        Character character = characters.get(0);
        turnTowards(character.getX(), character.getY());
    }
}
If your character is named different you will have to change that. For your shooting problem you could use this method:
//you first need a global variable;
private long time = System.currentTimeMillis();

public void shootAtCharacter() {
    if (System.currentTimeMillis() - time > 3000) {
        //this will make your tank shoot every 3 seconds. If you change the value in the if statement you can change the time.
        time = System.currentTimeMillis();
        getWorld().addObject(new Bullet(getRotation()), getX(), getY());
    }
}

//to use this method you now need the class Bullet;
//If you already got a class similar to this you just have to change the constructor;
//you just need the constructor of this class;
//If you need help concerning the rest of the class just ask.

public Bullet(int rotation) {
    setRotation(rotation);
}
manish1 manish1

2013/5/21

#
Thanks for ur reply! so when i compile the turnTowardsTank method it gives me this error: Non- static method getX() cannot be referenced from a static context and it highlights the Tank.getX() part Any ideas on how to fix this?
davmac davmac

2013/5/21

#
There's no Tank.getX() part in the code listed above? If you modified the code, post it!
manish1 manish1

2013/5/21

#
no, he said character.getX() my character is not called character its Tank
Gevater_Tod4711 Gevater_Tod4711

2013/5/21

#
You probably did this:
public void turnTowardsCharacter() {  
    List<Tank> tanks = getWorld().getObjects(Tank.class);  
    if (tanks != null && !tanks.isEmpty()) {  
        Tank tank = tanks.get(0);  
        turnTowards(Tank.getX(), Tank.getY());  
    }  
}  
If so you just have to change the line turnTowards(... into
turnTowards(tank.getX(), tank.getY());
manish1 manish1

2013/5/22

#
Oh my! It workks perfectly! Thank you so much! Ill be sure to give you credit in my code!
You need to login to post a reply.