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

2019/1/29

I am trying that an object removes an object which is touchung another Object???

Kili Kili

2019/1/29

#
I have a problem. I want that the class trefferpunkte ... the variable treffer if a ball is touching a patrone. If I write ball.isTouching(patrone.class) there is an error and I do not understand why. Here the Code of class trefferpunkte,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class trefferpunkte here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class trefferpunkte extends ball
{
    /**
     * Act - do whatever the trefferpunkte wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int treffer;
    
    public trefferpunkte(){
        setRotation(0);
        treffer=Greenfoot.getRandomNumber(80)+20;
        
        
        
    }
    
    public void act() 
    {
         ball ball = (ball) getWorld().getObjects(ball.class).get(0);
         patrone patrone = (patrone) getWorld().getObjects(patrone.class).get(0);
         
         if(ball!=null){
       if(ball.isTouching(patrone.class)){
            ball.removeTouching(patrone.class);
            treffer--;
        }
       
         }
        if(treffer==0){
            removeTouching(ball.class);
            getWorld().removeObject(this);
        }
        setLocation(xpos, ypos);
        Color trans = new Color(0, 0, 0, 0);
        setImage(new GreenfootImage(Integer.toString(treffer), 30, Color.BLACK, trans));
   // }    
}}

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

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball extends Actor
{
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int x;
    int x2;
    public static int i;
    public static int xpos;
    public static int ypos;
    private int trefferpunkte;
    
    public ball(){
        
        this.getImage().scale(60,60);
        
    setRotation(270);
    x=15;
    i=Greenfoot.getRandomNumber(2);
    trefferpunkte=100;
    if(i==0){
            x2=1;
        }
         if(i==1){
            x2=-1;
        }
    }
    public void act() 
    {
        
        
        
        xpos=getX()+x2;
        ypos=getY()+(x/5);
        
        
        if(isAtEdge()){
            x2=x2*-1;
        }
        
        
            setLocation(getX()+x2, getY()+(x/5));
        
       
            
        if(getY()<300){
            x++;;
            
        }
        
         if(getY()>380){
            x=x*-1;
        }
        
    
       
    }    
}
and patrone
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class kugeln here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class patrone extends kanone
{
    /**
     * Act - do whatever the kugeln wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public patrone(){
        this.getImage().scale(5,10);
    }
    
    public void act() 
    {
        setLocation(getX(), getY()-5);
        
        if(isAtEdge()){
            getWorld().removeObject(this);
        }
    }    
}
and here the error
danpost danpost

2019/1/29

#
A method with protected access can only be executed on 'this', which is implicitly understood if no object is explicitly given to execute the method on. That being said, to use isTouching on a ball object, it must be coded in the ball class. You can add a public method that returns a boolean value depending on the touching state in the ball class and call it from the other class.
You need to login to post a reply.