This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
Quzord
Quzord wrote ...

2017/3/20

Need help with Actor Class disappearing to top of image.

Quzord Quzord

2017/3/20

#
Basic Platform, but when my character jumps it disappears. Wondered if anyone could help fix it. public class Player extends Actor
public class Player extends Actor
{
    private int Timer;
    private int Jump_Height = 12;
    private int Walk_Speed = 3;
    private double Fall_Speed = 0.4;
    
    private boolean In_The_Air = false;
    private double Delta_X = 0;
    private double Delta_Y = 0;
    private int Ground_Height = getImage().getHeight()/2;
    private int Side_Width = getImage().getWidth()/2;
    private World myWorld;
    int World_Height;
    int World_Width;
    
    public void Added_To_World(World myWorld)
    {
        this.myWorld = myWorld;
        this.World_Height = myWorld.getHeight();
        this.World_Width = myWorld.getWidth();
    }
    
    public void act()
    {
        if (In_The_Air)
        {
            Fall();
        }
        else
        {
            Get_Command();
        }
        
        Move();
        int Pos_Y = getY()+10;
        
        if (Timer > 0)
        {
            Timer--;
        }
        
        if (Timer == 0 && Greenfoot.isKeyDown("space"))
        {
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, 250, Pos_Y);
            Timer = 30;
        }
    }
    
    private void Run (String Direction)
    {
        if (Direction =="left")
        {
            Delta_X = Walk_Speed*-1;
        }
        else
        {
            Delta_X = Walk_Speed*-1;
        }
    }
    
    private void Stop()
    {
        Delta_X = 0;
    }
    
    private void Jump()
    {
        Delta_Y += Jump_Height;
        In_The_Air = true;
    }
    
    private void Fall()
    {
        Delta_Y-=Fall_Speed;
    }
    
    private void Move()
    {
        double New_X = getX() + Delta_X;
        double New_Y = getY() - Delta_Y;
        
        Actor Platform_Below = getOneObjectAtOffset(0, Ground_Height, Platform.class);
        if (Platform_Below != null)
        {
            if (Delta_Y < 0)
            {
                Delta_Y = 0;
                In_The_Air = false;
                GreenfootImage platformImage = Platform_Below.getImage();
                int Top_Of_Platform = Platform_Below.getY() - platformImage.getHeight()/2;
                New_Y = - Ground_Height;
            }
        }
        else if (getY() >= World_Height - Ground_Height)
        {
            if (Delta_Y < 0)
            {
                Delta_Y = 0;
                In_The_Air = false;
                New_Y = - Ground_Height;
            }
        }
        else
        {
            In_The_Air = true;
        }
        
        setLocation((int)New_X, (int)New_Y);
    }
    
    private void Get_Command()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            Jump();
        }
    }
    
    public void act_Two()
    {
        if (isTouching(EnemyOne.class))
        {
            removeTouching(EnemyOne.class);
            getWorld().addObject(new EnemyOne(), 1000, 2);
            Level_1.HP.Add(-1);
            if (Level_1.HP.Get_Value() == 0)
            {
                Greenfoot.setWorld(new RetryWorld());
            }
        }
    }
}
Super_Hippo Super_Hippo

2017/3/20

#
0|0 is the top left corner. So to move up, you need to decrease the y-coordinate and vice versa.
Quzord Quzord

3 days ago

#
What does that even mean?
Quzord Quzord

3 days ago

#
My character can jump. The problem is when he does, when he's in the air after you press spacebar, my character disappears.
danpost danpost

3 days ago

#
After it disappears, pause the scenario, right-click on the background of the world and select the World class method 'getObjects'; enter 'Player.class' and press the enter key. In the inspection frame, inspect the list; if the size is one, then your player is still in the world -- select the first element in the list and inspect (or double-click the first element (# 0) -- check the location coordinates of the Player object found in the list.
Quzord Quzord

3 days ago

#
It's at 1. but the character(player.class) is not there. Just says null. and the int length is 10.
danpost danpost

3 days ago

#
Quzord wrote...
It's at 1. but the character(player.class) is not there.
It is there. Select the field under where you see int length is 10 and inspect that. You should get the player and see its x and y coordinates in the world.
Just says null.
Where did you see this, exactly?
the int length is 10
No matter. That is just the initial number of elements the list is created to hold (before any objects are added to the list).
You need to login to post a reply.