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

2019/6/21

How to make the object rotate with another

1
2
3
4
MisterUnknown MisterUnknown

2019/6/25

#
Thanks, now my code looks like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)
//import java.lang.String;
//import java.lang.Math;

/**
 * Ergänzen Sie hier eine Beschreibung für die Klasse Player.
 * 
 * @author (Ihr Name) 
 * @version (eine Versionsnummer oder ein Datum)
 */
public class Player extends Actor
{
    String leftKey, rightKey, upKey, downKey;
    //GreenfootImage myImage;
   // public int myPlayerNum = 0;
    int playerNum;
   // boolean didShoot = false;
    public double xVel = 0.0;
    public double yVel = 0.0;
    public double terminalSpeed = 5.0;
    public int counter;
    boolean playerContact = false;
    double currentForce = 0;
    //public Pipe pipe;
    Pipe pipe = new Pipe();
    //int shotCoolDown = 0;
    //String shootKey = "o";
    
    public Player(int num, String image, String left, String right, String up, String down)
    {
        //myImage = new GreenfootImage(image);
        //myPlayerNum = playerNum;
        playerNum = num;
        leftKey = left;
        rightKey = right;
        upKey = up;
        downKey = down;
        //didShoot = false;
        myImage.scale(50, 50);
        //setImage(myImage);
        
        if (num == 0)
        {
            setImage(new GreenfootImage("tank.png"));
            rightKey = "right";
            leftKey = "left";
            upKey = "up";
            downKey = "down";
        }
        else
        {
            setImage( new GreenfootImage("tank2.png"));
            rightKey = "d";
            leftKey = "a";
            upKey = "w";
            downKey = "s";
        }
        getImage.scale(50, 50);
        setImage(getImage());
    }
    /**
     * Act - tut, was auch immer Player tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    public void act() 
    {
        movementEngine();
        turnTowardsEnemy();
        wallBounce();
        calculateForce();
        doBulletHit(myPlayerNum);
        playerCollider();
        mutualmovement();
        shootStuff();
        //if (this.Player.getWorld() == null) { getWorld().removeObject(this); return;}
               }    
    public void mutualmovement()
    {
       
        //Pipe actor = (Pipe) getOneIntersectingObject(Pipe.class);
        //if (actor != null) {
           pipe.setLocation(this.getX(),this.getY());
        
        
    }
    public void turnTowardsEnemy()
    {
        if(playerNum == 1) {
         turnTowards(BattleWorld.p2X, BattleWorld.p2Y);
         }
         else {
             turnTowards(BattleWorld.p1X, BattleWorld.p1Y);
         }
         
        pipe.setRotation(this.getRotation());
    }
    
    public void movementEngine()
    {
        //int angle = getRotation();
        
        setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        if(Greenfoot.isKeyDown(rightKey))
            xVel += 0.5;
        if(Greenfoot.isKeyDown(leftKey))
            xVel -= 0.5;
        if(Greenfoot.isKeyDown(upKey))
            yVel -= 0.5;
        if(Greenfoot.isKeyDown(downKey))
            yVel += 0.5;
        friction();
        terminalVelocity();
    }
    
     public void friction()
    {
        if(xVel >= 0)
            xVel -= 0.125;
        if(xVel <= 0)
            xVel += 0.125;
        if(yVel >= 0)
            yVel -= 0.125;
        if(yVel <= 0)
            yVel += 0.125;
    }
    
    public void terminalVelocity()
    {
        if(xVel >= terminalSpeed)
            xVel = terminalSpeed;
        if(xVel <= -terminalSpeed)
            xVel = -terminalSpeed;
        if(yVel >= terminalSpeed)
            yVel = terminalSpeed;
        if(yVel <= -terminalSpeed)
            yVel = -terminalSpeed;
    }
    public void bleed(int particles, int x, int y)
    {
        for(int a = 0; a < particles; a++) {
            bleed(x, y);
        }
    }
    public void bleed(int x, int y)
    {
        getWorld().addObject(new Particle("blood", ((Greenfoot.getRandomNumber(21)-10)/4.0), ((Greenfoot.getRandomNumber(21)-10)/5.0)), x, y);
    }
        public boolean atWorldLeft()
    {
        if(getX() < 20)
            return true;
        else return false;
    }
    
    public boolean atWorldRight()
    {
        if(getX() > getWorld().getWidth() - 20)
            return true;
        else return false;
    }
    public boolean atWorldTop()
    {
        if(getY() < 20)
            return true;
        else return false;
    }
    
    public boolean atWorldBottom()
    {
        if(getY() > getWorld().getHeight() - 20)
            return true;
        else return false;
    }
    
    
    public void bleed()
    {
        getWorld().addObject(new Particle("blood", ((Greenfoot.getRandomNumber(21)-10)/4.0), ((Greenfoot.getRandomNumber(21)-10)/5.0)), getX(), getY());
    }
    public void wallBounce()
    {
        if(atWorldRight() && xVel > 0) {
            xVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
        if(atWorldLeft() && xVel < 0) {
            xVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
        if(atWorldTop() && yVel < 0) {
            yVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
        if(atWorldBottom() && yVel > 0) {
            yVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
    }
    public void calculateForce()
    {
        currentForce = Math.hypot(Math.abs(xVel), Math.abs(yVel));
        
    }
    public void playerCollider()
    {
        if (getWorld() == null) return;
        if(isTouching(Player.class)) {
            playerContact = true;
            Actor oPlayer = (Actor) getOneIntersectingObject(Player.class);
            int oPlayerX = oPlayer.getX();
            int oPlayerY = oPlayer.getY();
            double k = 1.5 * currentForce;
            int angle = getRotation();
            if (oPlayerX > getX()) {
                 if(oPlayerY < getY()) {
                    
                    xVel = (-1) * (k) * Math.cos(Math.toRadians(360-angle));
                    yVel = (1) * (k) * Math.sin(Math.toRadians(360-angle));
                }
                else {
                    
                    xVel = (1) * (k) * Math.cos(Math.toRadians(angle-180));
                    yVel = (1) * (k) * Math.sin(Math.toRadians(angle-180));
                }
            }
            else {
                if(oPlayerY < getY()) {
                    
                    xVel = (1) * (k) * Math.cos(Math.toRadians(180-angle));
                    yVel = (-1) * (k) * Math.sin(Math.toRadians(180-angle));
                }
                else {
                    
                    xVel = (-1) * (k) * Math.cos(Math.toRadians(angle));
                    yVel = (-1) * (k) * Math.sin(Math.toRadians(angle));
                }
            }
            
        }
        else {
            playerContact = false;
        }
        
}
public void doBulletHit(int playerNum)
{
    World myWorld = getWorld();
        BattleWorld battleWorld = (BattleWorld)myWorld;
        //myPlayerNum = playerNum;
    if  (isTouching(Bullet.class) && !(didShoot))
        {
            
            Bar bar = battleWorld.getBar(playerNum);
            
            Actor bull = (Actor) getOneIntersectingObject(Bullet.class);
            bleed((Greenfoot.getRandomNumber(3)+2), bull.getX(), bull.getY());
            getWorld().removeObject(bull);
           
            
            bar.loseHealth();
            if(bar.health <=0)
            {
                GameOver gameover = new GameOver(myPlayerNum);
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
                if (++counter == 5) Greenfoot.setWorld(new TitleScreen());
            }
            
        }
        
        }
   
    //public void setShootKey(String key)
        //{
            //shootKey = key;
        //}
           protected void addedToWorld(World world)
{
    world.addObject(pipe, getX(), getY());
}

public class Pipe extends Actor 
{
    String shootKey;
    
    public Pipe()
    {
        
        shootKey = this.Player.playerNum == 0 ? "e": "o";
        GreenfootImage img = new GreenfootImage("Rohr"+(this.Player.playerNum+1)+".PNG");
        img.scale(25, 25);
        setImage(img);
    }
}
public void act()
{
    if (this.Player.getWorld() == null) { getWorld().removeObject(this); return; }
    shootStuff();
    
}
public void shootStuff()
{
    if (shotCoolDown > 0) shotCoolDown--;
    else if (Greenfoot.isKeyDown(shootKey))
    {
        getWorld().addObject(new Bullet(getRotation()), getX(), getY());
        shotCoolDown = 20;
    }
}
}
It cannot find symbol in
myImage.scale(50, 50);
same here
getImage.scale(50, 50);
doBulletHit(myPlayerNum);
if  (isTouching(Bullet.class) && !(didShoot))
GameOver gameover = new GameOver(myPlayerNum);
shootKey = this.Player.playerNum == 0 ? "e": "o";
public void act()
 if (this.Player.getWorld() == null) { getWorld().removeObject(this); return; }
 if (shotCoolDown > 0) shotCoolDown--;
 else if (Greenfoot.isKeyDown(shootKey))
shotCoolDown = 20;
The errors occur to: shotCoolDown, shootKey, Player, act(), myPlayerNum, didShoot,getImage and in total I have 14 errors in that class, while in my main-world 2, eventhough I am currently unable to check to the programm having crashed Note: Nearly every error is a "cannot find symbol - variable" type of error, while act() tells me that method act() is already defined in class Player
danpost danpost

2019/6/25

#
Move line 294 to the end. This should help most of the errors Place the following at line 285:
int shotCoolDown = 0;
Remove line 39. Line 58 should be:
getImage().scale(50, 50);
Line 71 should be:
doBulletHit(playerNum)
The only thing left should be the didShoot code which still needs to be worked on.
MisterUnknown MisterUnknown

2019/6/25

#
I gave up on this, so I am currently trying to make my game work without the pipes but again I have an error, I didnt had the last time here: Player player = new Player(0); Player player2 = new Player(1); It tells me that the constructor Player in class Player cannot be applied to given types; required; int Java.lang.string,java.lang.string,java.lang.string ... reason: actual and formal argument lists differ in length
MisterUnknown MisterUnknown

2019/6/25

#
What I learned: programming is one of the most frustratic things I did today
MisterUnknown MisterUnknown

2019/6/25

#
My current issue is that doesnt matter which shootkey I push, only one works and its always being shot by one tank
MisterUnknown MisterUnknown

2019/6/25

#
And on the latest file with the implemented pipes, I recently fixed most of the errors, so I only have Player of
this.Player.getWorld()
and
this.Player.playerNum 
as an error
danpost danpost

2019/6/26

#
MisterUnknown wrote...
And on the latest file with the implemented pipes, I recently fixed most of the errors, so I only have Player of
this.Player.getWorld()
and
this.Player.playerNum 
as an error
I had it backwards. Try:
Player.this.getWorld()
and
Player.this.playerNum 
Although, I think you can use just playerNum for the second one.
MisterUnknown MisterUnknown

2019/6/26

#
Thanks :). Now both pipes dont move, just rotate and only one shootkey works, which triggers both pipes
You need to login to post a reply.
1
2
3
4