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

2014/2/7

deleting an actor of another class

VishalChothani VishalChothani

2014/2/7

#
Hi, I want to delete a message after i click on the gumball machine, but I am not sure how to do it. here is my code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Coin extends Actor { public void act() { int mouseX, mouseY ; if(Greenfoot.mouseDragged(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); mouseX=mouse.getX(); mouseY=mouse.getY(); setLocation(mouseX, mouseY); if((mouseX >= 374 && mouseX <= 417) && (mouseY >= 238 && mouseY <= 254)) { World world; world = getWorld(); world.removeObject(this); Greenfoot.playSound("coinDrop.wav"); Actor msg = new Message("Coin is Inserted"); world.addObject(msg, 390, 20); // ADDED the objecj in the world GumBallMachine machine = new GumBallMachine(); machine.getCoin(this,msg); } } } } ========================================= import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Message extends Actor // To create message { public Message(String text) { GreenfootImage worldMsg = new GreenfootImage(text,20,Color.cyan,Color.black); setImage(worldMsg); } } ========================================= import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; /** * Write a description of class GumBallMachine here. * * @author (your name) * @version (a version number or a date) */ public class GumBallMachine extends Actor { static public String validateCoin ; public GumBallMachine() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 100, image.getHeight() - 100); setImage(image); } public void getCoin(Coin c, Actor msg) { validateCoin = c.toString(); validateCoin = validateCoin.substring(0, validateCoin.indexOf("@")); getWorld().removeObject(msg); // WANT TO DELETE THE MSG here (but this is not working) } public void act() { if(Greenfoot.mouseClicked(this)) { int mouseX,mouseY; MouseInfo mouse = Greenfoot.getMouseInfo(); mouseX=mouse.getX(); mouseY=mouse.getY(); if((mouseX >= 350 && mouseX <= 420) && (mouseY >= 300 && mouseY <= 375)) { //System.out.println(validateCoin); Inspector inspect = new Inspector(); inspect.coinCheck(validateCoin); } } } } Please comment if anyone knows the answer. Thank you.
danpost danpost

2014/2/8

#
I saw your 'getCoin' method, whose code looks to be ok; so where is this method being called from? what is the block of code, what class is it in and what type of class is it (and what fields are relevant ...etc)? Oh, and please use the 'code' tag below the 'Post a reply' input box to insert your code.
VishalChothani VishalChothani

2014/2/8

#
Thanks for the reply. It is in coin class. Sorry for not "Code" tag, I am relatively new to this site. Whenever I try to remove it from the world it gives me NULL POINT EXCEPTION.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Coin extends Actor
{
    public void act() 
    {
        int mouseX, mouseY ;
        
        if(Greenfoot.mouseDragged(this)) 
        {          
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            mouseX=mouse.getX();  
            mouseY=mouse.getY();  
            setLocation(mouseX, mouseY);   
            
            if((mouseX >= 374 && mouseX <= 417) && (mouseY >= 238 && mouseY <= 254))
            {
                World world;
                world = getWorld();
                world.removeObject(this);
                Greenfoot.playSound("coinDrop.wav");
                
                Actor msg = new Message("Coin is Inserted");  
                world.addObject(msg, 390, 20);     // ADDED the objecj in the world
                
                GumBallMachine machine = new GumBallMachine();                
                machine.getCoin(this,msg);    // GET COIN IS CALLED FROM HERE
                
            }
        } 
        
        
    }        
}
danpost danpost

2014/2/8

#
Do you not already have a GumBallMachine object in the world? Why are you creating another one and giving the coin to it (the one that is not in the world)? You need to give the coin to the machine that is in the world or 'getWorld' (to remove the coin) will return 'null'; hence, your NullPointerException. Do not create a new GumBallMachine object in the Coin class; just use the following to have the machine that is already in the world get the coin:
((GumBallMachine)world.getObjects(GumBallMachine.class).get(0)).getCoin(this, msg);
VishalChothani VishalChothani

2014/2/8

#
Thank a lot for the explanation, It worked :)
VishalChothani VishalChothani

2014/2/8

#
hi Dan, I how 2 questions 1) How can I Highlight an image which is there in the world when a condition occurs 2) How can I place a image on top of other image thank you in advance for the help.
danpost danpost

2014/2/9

#
VishalChothani wrote...
1) How can I Highlight an image which is there in the world when a condition occurs 2) How can I place a image on top of other image
1) What kind of actor is it? what kind of image does it display? what condition will you use to trigger highlighting? how is the image to change when highlighting occurs? 2) Do you mean one actor object on top of another? are they of the same type or different? do you always want the one type on top of the other? what are the class names of the actors?
VishalChothani VishalChothani

2014/2/9

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

public class Inspector extends Employee
{
    
    public Inspector()
    {
        GreenfootImage image = getImage();  
        image.scale(image.getWidth() - 190, image.getHeight() - 190);  
        setImage(image);
    }
    
    public void coinCheck(String coin) 
    {

        if(coin.equals("FakeQuarter"))
        {
            getWorld().removeObjects(getWorld().getObjects(Message.class));
            Message msg = new Message("Fake!!!! ");  
            getWorld().addObject(msg, 390, 20);   
        }
        
        else if(coin.equals("Penny"))
        {
            getWorld().removeObjects(getWorld().getObjects(Message.class));
            Message msg = new Message("Its a Penny, NOT ENOUGH");  
            getWorld().addObject(msg, 390, 20);
        }
        
        else
        {
            getWorld().removeObjects(getWorld().getObjects(Message.class));
            Message msg = new Message("Its a Quarter");  
            getWorld().addObject(msg, 390, 20);
            
            int number = Greenfoot.getRandomNumber(2);            
            Picker pic = new Picker();
            ((Picker)getWorld().getObjects(Picker.class).get(0)).decide(number);
            
        }
    }    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class RandomPicker here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RandomPicker extends Picker
{
    
    public RandomPicker()
    {
        GreenfootImage image = getImage();  
        image.scale(image.getWidth() - 190, image.getHeight() - 190);  
        setImage(image);
    }
    
    public void giveGumball(int picker) 
    {
        picker = 0;
        switch(picker)
        {
            case 0 :            
                GreenfootImage image1 = new GreenfootImage("blue-gumball.png");
                image1.scale(image1.getWidth() - 40, image1.getHeight() - 30);
                getWorld().getBackground().drawImage(image1,590,100); 
                Greenfoot.delay(100);
                //getWorld().removeObjects(getWorld().getObjects(RandomPicker.class)); 
                break;

            case 1 :            
                GreenfootImage image2 = new GreenfootImage("green-bumball.png");
                image2.scale(image2.getWidth() - 120, image2.getHeight() - 120);
                getWorld().getBackground().drawImage(image2,590,100);        
                Greenfoot.delay(100);
                break;

            case 2 :            
                GreenfootImage image3 = new GreenfootImage("red-gumball.png");
                image3.scale(image3.getWidth() - 60, image3.getHeight() - 40);
                getWorld().getBackground().drawImage(image3,590,100);        
                Greenfoot.delay(100);
                break;
                
            }                
                
    }
            
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GreenPicker here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GreenPicker extends Picker
{
    
    
    public GreenPicker()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 200, image.getHeight() - 200);
        setImage(image);
    }
    
    public void giveGumball() 
    {
        GreenfootImage image1 = new GreenfootImage("green-bumball.png");
        image1.scale(image1.getWidth() - 100, image1.getHeight() - 100);
        getWorld().getBackground().drawImage(image1,550,430);
        Greenfoot.delay(100);
        //getWorld().removeObjects(getWorld().getObjects(GreenPicker.class)); 
    }    
}
1) I want the ball should be in the hands of the white shirt Employee 2) For say 5sec i want the focus to be on Inspector and no other object should be displayed. Please help me out with this.
danpost danpost

2014/2/9

#
I guess you need to create an actor that covers the screen and only show the white shirt Employee. It can remove itself after the 'say 5sec'.
VishalChothani VishalChothani

2014/2/10

#
thanx a lot Dan for helping!!!! I will try that out. Appreciated
You need to login to post a reply.