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

2019/6/18

NEED HELP ASAP Spawning an Actor when an actor is clicked FOR ASSESSMENT

HydraLord257 HydraLord257

2019/6/18

#
Hi!! I am trying to make an actor spawn at a specific area when an actor is clicked which is a button for a quiz. PLSS Help me!!
danpost danpost

2019/6/18

#
Need context and codes (current and attempted).
HydraLord257 HydraLord257

2019/6/18

#
public class Answer1c extends Education
{
    /**
     * Act - do whatever the Answer1c wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))
        Greenfoot.setImage(//image of the correct answer
        getX, getY
        Greenfoot.playSound("CorrectAnswer.wav");
    }    
}
This was still a draft that I tried to do.
danpost danpost

2019/6/18

#
HydraLord257 wrote...
<< Code Omitted >> This was still a draft that I tried to do.
Can you explain what you think each line should do in this code? (mainly lines 9 thru 12)
HydraLord257 HydraLord257

2019/6/18

#
Basically, I am trying to once the actor is pressed which is a button. Once it is pressed an actor will be spawned at a specific location. I did not know what to mainly do on how to actually write it. It also plays a sound when it is also pressed. Hope its enough :|
danpost danpost

2019/6/18

#
That really did not explain what you think each line does. You need to do more than one thing when the actor is clicked -- spawn an actor and play a sound. Therefore, you will need a block of code for the if statement:
if (Greenfoot.mouseClicked(this))
{
    // codes to process
}
Now, "spawn" means "create and add to the world". You create an object by using the new keyword. For example (not your actual code -- just showing use of the keyword):
Actor message = new Message("Correct");
Then, you add it into the world by using the addObject method on your World object:
getWorld().addObject(message, 300, 200);
The getWorld method gets the World object that the button is in. Your lines 10 and 11 make no sense at all. There is no setImage method in the Greenfoot class and getX and getY are meaningless as written. You should probably skim over the API documentation of the classes provided in the greenfoot package to see what methods there are in each.
You need to login to post a reply.