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

2024/3/31

getX() getY() public variable actor not in world

power_coding power_coding

2024/3/31

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class background extends Actor
{
    int x = getX();
    int y = getY(); 
     public void act() 
    {
        setRotation(90);
        World W = getWorld();
        
          if(Greenfoot.isKeyDown("up")){
            move(2);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-2);
        }
        
    }
    public int getXB(){
        return x;
    }
    public int getYB(){
        return y;
    }
    
}
error message i feel like a idiot but pleas help me
Spock47 Spock47

2024/3/31

#
Two things are relevant here: 1. The variables of an object get initialized at creation of the object. 2. An actor gets put into the world only after it has been created. Therefore, line 11 "int x = getX();" will call method getX() when the background actor gets created. At this point, the actor is not yet in the world, therefore getX will throw the mentioned error. Question for solving the problem: What do you need methods getXB and getYB for? Can't you just use getX() and getY() there directly? Live long and prosper, Spock47
power_coding power_coding

2024/4/1

#
thx for trying to help i need xb and yb to share the x and y location to other actor's
danpost danpost

2024/4/2

#
power_coding wrote...
thx for trying to help i need xb and yb to share the x and y location to other actor's
The first problem (as already addressed) is that the actor is not in the world when the variables are initialized; so, errors will ensue. The second one is that the variables are never updated. The lines 11 and 12 are only execute once -- during the creation of the object (as already stated). The values are only initially set by what follows the equal signs; they do not automatically update when the actor moves. Since the actor(s) needing this one's coordinates must already have a reference to this actor, then you have no need for extra code in this actor's class. Just use something like this in the action code of those actor(s):
int backgroundX = referenceName.getX();
int backgroundY = referenceName.getY();
(where referenceName is the variable name holding the background instance in those other classes). The class can simply be the following:
import greenfoot.*;

public class background extends Actor
{
    public void act() {
        setRotation(90);
        if (Greenfoot.isKeyDown("up")) {
            move(2);
        }
        if (Greenfoot.isKeyDown("down")) {
            move(-2);
        }
    }
}
power_coding power_coding

2024/4/3

#
but then i get the error messege non static cannot be referenced from a static cotext
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class longen_middel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class longen_middel extends longen
{
    int backgroundX = background.getX();
    int backgroundY = background.getY();

    public void act()
    {
        World W = getWorld();
        
        setRotation(90);
        if(Greenfoot.isKeyDown("up")){
            setLocation(153, backgroundX+127);
          }
        if(Greenfoot.isKeyDown("down")){
            setLocation(153, backgroundX+127);
          }
        
        
    }
    
    }

danpost danpost

2024/4/4

#
power_coding wrote...
but then i get the error messege non static cannot be referenced from a static cotext << Code Omitted >>
Please provide the code in your background class. Would be good to provide the code for your world (the class extending World) also. In addition, please provide the code to the longen class.
power_coding power_coding

2024/4/6

#
background:
import greenfoot.*;
 
public class background extends Actor
{
    public void act() {
        setRotation(90);
        if (Greenfoot.isKeyDown("up")) {
            move(2);
        }
        if (Greenfoot.isKeyDown("down")) {
            move(-2);
        }
    }
}
world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class main extends World
{
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public main()
    {    
        
        super(316, 400, 1); 
        prepare();
    }
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(new background(),          153, 400);
        addObject(new longen_middel(),    153, 273);
        addObject(new longen_top(),       153, 215);
        addObject(new longen_onderkant(), 153, 357);
    }
}
danpost danpost

2024/4/8

#
danpost wrote...
... In addition, please provide the code to the longen class.
I guess you missed the ending of my last post.
power_coding power_coding

2024/4/10

#
i don't have a longen class only longen middel top and onderkant
danpost danpost

2024/4/11

#
power_coding wrote...
i don't have a longen class only longen middel top and onderkant
??? Please provide those for inspection.
power_coding power_coding

2024/4/11

#
power_coding wrote...
but then i get the error messege non static cannot be referenced from a static cotext
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class longen_middel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class longen_middel extends longen
{
    int backgroundX = background.getX();
    int backgroundY = background.getY();

    public void act()
    {
        World W = getWorld();
        
        setRotation(90);
        if(Greenfoot.isKeyDown("up")){
            setLocation(153, backgroundX+127);
          }
        if(Greenfoot.isKeyDown("down")){
            setLocation(153, backgroundX+127);
          }
        
        
    }
    
    }

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class longen_onderkant here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class longen_onderkant extends longen
{
    /**
     * Act - do whatever the longen_onderkant wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        World W = getWorld();
        setRotation(90);
         if (getY() < 1) {
          W.removeObjects(W.getObjects(longen_onderkant.class));
        }
        if(Greenfoot.isKeyDown("up")){
            move(2);
          }
        if(Greenfoot.isKeyDown("down")){
            move(-2);
          }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class longen_top here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class longen_top extends longen
{
    /**
     * Act - do whatever the longen_top wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        World W = getWorld();
        setRotation(90);
         if (getY() < 1) {
          W.removeObjects(W.getObjects(longen_top.class));
        }
        if(Greenfoot.isKeyDown("up")){
            move(2);
          }
        if(Greenfoot.isKeyDown("down")){
            move(-2);
          }
    }
}
but i did not do thes becaus thes have to be the same as longen_middel
danpost danpost

2024/4/12

#
power_coding wrote...
<< Code Omitted >>
All these classes seem to extend a longen class. You certainly must have one, or the code checking would have told you it was missing.
power_coding power_coding

2024/4/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class longen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class longen extends organen
{
    /**
     * Act - do whatever the longen wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }
}
i just have this class for sorting
danpost danpost

2024/4/12

#
power_coding wrote...
i just have this class for sorting
And this one extends the organen class.
danpost danpost

2024/4/12

#
I did notice one issue. In both the longen_onderkant and longen_top classes, the check to remove all instances of its type from the world should be the last if in the act block. The first attempt to execute a move call after all instances were removed will fail for actor not in world. My long-standing concern is the reference to background.in lines 11 and 12 in the longen_middel class. I do not see how it could reference an instance of the background class (at lest not straight off). As far as I can tell, it is just a class name (which you cannot perform a getX or getY method on. Until I know what all is involved, I cannot truly know what to do to resolve the issue. That is why I need to see these parent classes.
You need to login to post a reply.