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

2018/8/5

getX, getY problems

Ticker175 Ticker175

2018/8/5

#
Hey guys im having trouble with the getX and getY commands I'm trying to get the position of one actor and turn it into an integer for use is another actor. so i've managed to get actor1's position as a integer: I'm unseeing, (public int pacX public int pacY) as the integers for actor1 position and when i inspect the actor it is correct, but when i when i inspect actor2 it shows both pacX and pacY as 0. i have public int pacx; public int pacy; in both classes, not sure if i am just missing something basic or what. Thanks Thomas
danpost danpost

2018/8/5

#
Ticker175 wrote...
Hey guys im having trouble with the getX and getY commands
Need to see both classes with pacx and pacy plus the code used to acquire their values from the other class.
Ticker175 Ticker175

2018/8/5

#
here is my code for actor1
    public int pacx;
    public int pacy;
    public double locationx;
    public double locationy;
    
    public void act()
    {
        //Get the location of Pacman and assign it to integers
            Location();
    }
    
    public void Location()
    {
        locationx = (getX());
        pacx = (int)locationx;
        
        locationy = (getY());
        pacy = (int)locationy;
    }
here is my actor2 code
import greenfoot.*;

public class Blinky extends Ghosts
{
    public int pacx;
    public int pacy;
    public int Tracking = 0;
    private int rotation = 0;
    private int left = 0;
    private int up = 0;
    private int right = 0;
    private int down = 0;
    public int moveb=0, movebb=-2;
    public int act1=1;
    public int exc = 0;
    public int SHIT=0;
    public int welp=0;
    
    public int Alt_Distance;
    public int firstTime = 0;
    public int Distance1 = -13;
    public int Distance2 = -13;
    public int Distance3 = -13;
    public int Distance4 = -13;
    public int Distance1_Direction = 0;
    public int Distance2_Direction = 0;
    public int Distance3_Direction = 0;
    
    public double distance;
    
    
    
    // aims directly for pacman and speeds up when half of the dots are collected
    public void act()
    {
        if (Tracking == 0){
            setLocation(getX()+moveb, getY()+movebb);
            
            Actor Wall = getOneIntersectingObject(Wall.class);
            if (Wall != null) {
                setLocation(getX()-moveb, getY()-movebb);
            }
        }
        // up=0;
        // down=0;
        // left=0;
        // right=0;
        
        if (isTouching(Entry_Intercection.class)){
            Distance1 = -13;
            Distance2 = -13;
            Distance3 = -13;
            Distance4 = -13;
            Distance1_Direction = 0;
            Distance2_Direction = 0;
            Distance3_Direction = 0;
        }
        else if (isTouching(Intercection.class)){
            if (act1==1){
                up=0;
                down=0;
                left=0;
                right=0;
                
                firstTime = 0;
                Alt_Distance = 0;
                Distance1 = -13;
                Distance2 = -13;
                Distance3 = -13;
                Distance4 = -13;
                
                Pathfinding();
                
                checkRight();
                checkLeft();
                checkDown();
                checkUp();
                
                if (exc == 1)
                {
                    if (right+left+up+down>=2)
                    {
                        Organisation();
                    }
                    else
                    {
                        Execute();
                    }
                }
                else{
                    //no\\
                }
            }
        }
    }
    
    
    
    public void Pathfinding()
    {
        if (isTouching(TSticky.class)){
            left=2;
        }
        if (isTouching(BSticky.class)){
            left=2;
        }
        if (isTouching(GSticky.class)){
            right=2;
        }
        
        if (isTouching(Entry_Intercection.class)){
            //no\\
        }
        else if (isTouching(Intercection.class))
        {
            if (moveb==2) //right
            {
                if (isTouching(Intercection.class)){
                    // right=2;
                    left=2;
                    exc=1;
                }
            }
            else if (movebb==2) //down
            {
                if (isTouching(Intercection.class)){
                    // down=2;
                    up=2;
                    exc=1;
                }
            }
            else if (moveb==-2) //left
            {
                if (isTouching(Intercection.class)){
                    // left=2;
                    right=2;
                    exc=1;
                }
            }
            else if (movebb==-2) //up
            {
                if (isTouching(Intercection.class)){
                    // up=2;
                    down=2;
                    exc=1;
                }
            }
        }
    }
    
    
    public void checkRight()
    {
        Actor Thing = getOneObjectAtOffset(20,0,Wall.class);
        if(Thing==null)
        {
            if (right == 2)
            {
                right=0;
            }
            else if (right == 0){
                right=1;
            }
        }
        else{
            right=0;
        }
    }
    public void checkUp()
    {
        Actor Thing = getOneObjectAtOffset(0,-20,Wall.class);
        if(Thing==null)
        {
            if (up == 2)
            {
                up=0;
            }
            else if (up == 0){
                up=1;
            }
        }
        else{
            up=0;
        }
    }
    public void checkLeft()
    {
        Actor Thing = getOneObjectAtOffset(-20,0,Wall.class);
        if(Thing==null)
        {
            if (left == 2)
            {
                left=0;
            }
            else if (left == 0){
                left=1;
            }
        }
        else{
            left=0;
        }
    }
    public void checkDown()
    {
        Actor Thing = getOneObjectAtOffset(0,20,Wall.class);
        if(Thing==null)
        {
            if (down == 2)
            {
                down=0;
            }
            else if (down == 0){
                down=1;
            }
        }
        else{
            down=0;
        }
    }
    
    
    
    public void Execute()
    {
        if (isTouching(Entry_Intercection.class))
        {
            //NOPE\\
        }
        else if (isTouching(Intercection.class))
        {
            if (up == 1)//check up
            {
                if (isTouching(Intercection.class)){
                    moveb=0;
                    movebb=-2;
                    
                    exc=0;
                }
            }
            else if (left == 1)//check left
            {
                if (isTouching(Intercection.class)){
                    moveb=-2;
                    movebb=0;
                    
                    exc=0;
                }
            }
            else if (right == 1)//check right
            {
                if (isTouching(Intercection.class)){
                    moveb=2;
                    movebb=0;
                    
                    exc=0;
                }
            }
            else if (down == 1)//check down
            {
                if (isTouching(Intercection.class)){
                    moveb=0;
                    movebb=2;
                    
                    exc=0;
                }
            }
            else
            {
                SHIT=1;
            }
        }
    }
    
    public void Organisation()
    {
        if (right==1){
            distance = Math.hypot(pacx - getX()+30 , pacy - getY());
            Distance1 = (int)distance;
            Distance1_Direction = 1;
        }
        if (left==1){
            double distance = Math.hypot(pacx - getX()-30 , pacy - getY());
            if (Distance1 == -13)
            {
                Distance1 = (int)distance;
                Distance1_Direction = 2;
            }
            else
            {
                Distance2 = (int)distance;
                Distance2_Direction = 2;
            }
        }
        if (up==1){
            double distance = Math.hypot(pacx - getX() , pacy - getY()-30);
            if (Distance1 == -13)
            {
                Distance1 = (int)distance;
                Distance1_Direction = 3;
            }
            else if (Distance2 == -13)
            {
                Distance2 = (int)distance;
                Distance2_Direction = 3;
            }
            else
            {
                Distance3 = (int)distance;
                Distance3_Direction = 3;
            }
        }
        if (down==1){
            double distance = Math.hypot(pacx - getX() , pacy - getY()+30);
            if (Distance2 == -13)
            {
                Distance2 = (int)distance;
                Distance2_Direction = 4;
            }
            else if (Distance3 == -13)
            {
                Distance3 = (int)distance;
                Distance3_Direction = 4;
            }
            else
            {
                Distance4 = (int)distance;
            }
        }
        Calculations();
    }
    
    public void Calculations()
    {
        if (right+left+up+down==2)
        {
            if (Distance2 != -13)
            {
                if (Distance1-Distance2>=0)//if true then distance2 is smaller/better
                {
                    if (Distance2_Direction == 1)
                    {
                        right = 1;
                        left = 0; up = 0; down = 0;
                        Execute();
                    }
                    else if (Distance2_Direction == 2)
                    {
                        left = 1;
                        right = 0; up = 0; down = 0;
                        Execute();
                    }
                    else if (Distance2_Direction == 3)
                    {
                        up = 1;
                        right = 0; left = 0; down = 0;
                        Execute();
                    }
                    else
                    {
                        down = 1;
                        right = 0; left = 0; up = 0;
                        Execute();
                    }
                }
                else//if true then distance1 is smaller/better
                {
                    if (Distance1_Direction == 1)
                    {
                        right = 1;
                        left = 0; up = 0; down = 0;
                        Execute();
                    }
                    else if (Distance1_Direction == 2)
                    {
                        left = 1;
                        right = 0; up = 0; down = 0;
                        Execute();
                    }
                    else if (Distance1_Direction == 3)
                    {
                        up = 1;
                        right = 0; left = 0; down = 0;
                        Execute();
                    }
                    else
                    {
                        down = 1;
                        right = 0; left = 0; up = 0;
                        Execute();
                    }
                }
            }
        }
        else
        {
            if (Distance3 != -13)
            {
                if (Distance1-Distance2>=0)//if true then distance2 is smaller/better
                {
                    if (Distance2-Distance3>=0)//if true then distance3 is smaller/better
                    {
                        if (Distance3_Direction == 1)
                        {
                            right = 1;
                            left = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance3_Direction == 2)
                        {
                            left = 1;
                            right = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance3_Direction == 3)
                        {
                            up = 1;
                            right = 0; left = 0; down = 0;
                            Execute();
                        }
                        else
                        {
                            down = 1;
                            right = 0; left = 0; up = 0;
                            Execute();
                        }
                    }
                    else//if true then distance2 is smaller/better
                    {
                        if (Distance2_Direction == 1)
                        {
                            right = 1;
                            left = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance2_Direction == 2)
                        {
                            left = 1;
                            right = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance2_Direction == 3)
                        {
                            up = 1;
                            right = 0; left = 0; down = 0;
                            Execute();
                        }
                        else
                        {
                            down = 1;
                            right = 0; left = 0; up = 0;
                            Execute();
                        }
                    }
                }
                else//if true then distance1 is smaller/better
                {
                    if (Distance1-Distance3>=0)//if true then distance3 is smaller/better
                    {
                        if (Distance3_Direction == 1)
                        {
                            right = 1;
                            left = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance3_Direction == 2)
                        {
                            left = 1;
                            right = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance3_Direction == 3)
                        {
                            up = 1;
                            right = 0; left = 0; down = 0;
                            Execute();
                        }
                        else
                        {
                            down = 1;
                            right = 0; left = 0; up = 0;
                            Execute();
                        }
                    }
                    else//if true then distance1 is smaller/better
                    {
                        if (Distance1_Direction == 1)
                        {
                            right = 1;
                            left = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance1_Direction == 2)
                        {
                            left = 1;
                            right = 0; up = 0; down = 0;
                            Execute();
                        }
                        else if (Distance1_Direction == 3)
                        {
                            up = 1;
                            right = 0; left = 0; down = 0;
                            Execute();
                        }
                        else
                        {
                            down = 1;
                            right = 0; left = 0; up = 0;
                            Execute();
                        }
                    }
                }
            }
        }
    }
    
    
    
}
Super_Hippo Super_Hippo

2018/8/6

#
Those variables aren't global. Both classes have their own variables. If that wouldn't be the case, you would always need to check every class in your project if you want to create a variable with a name which isn't used yet. In fact, even every object (if you create several Pac-Mans or Ghosts have their individual variables. In code in "actor1" really doesn't make any sense at all.
Ticker175 Ticker175

2018/8/6

#
i only included the code that was needed in actor 1 i removed the irrelevant code just for simplicity. i have needed to remake most of actor2/blinkys code but i will still need to be able to get an integer from actor1/pacman to actor2/blinky could u help me with how to do that Thanks
danpost danpost

2018/8/7

#
I do not see a need for the pacx and pacy fields. What you really need to do is acquire a reference to the Pacman object in the class of the ghost. It might be best to do that in your Ghosts class as a static field. You can make use of the addedToWorld method to keep it up do date:
// in Ghosts class
protected static Actor pac;

protected void addedToWorld(World world)
{
    if (pac == null || pac.getWorld() == null || pac.getWorld() != world) pac = (Actor)world.getObjects(Pacman.class).get(0);
}
In your world class, make sure that the Pacman object is added into the world before any of the ghosts. With this, all codes given for actor1 above can be removed, pacx and pacy in the Ghosts subclasses can be removed and you can just use:
int pacx = pac.getX();
int pacy = pac.getY();
in the Ghosts subclasses' act methods (or any other method) to get the current values on the fly. Note: really, you do not need to set variables to the values -- just use the code to the right of the equal signs when needed.
Ticker175 Ticker175

2018/8/7

#
Ok awesome thanks for the help =)
You need to login to post a reply.