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

2012/12/14

Click to add objects

super_noob super_noob

2012/12/14

#
I don't know the correct coding to be able to click in the world and add a new object. Can someone help?
Zamoht Zamoht

2012/12/14

#
if (Greenfoot.mouseClicked(null)) { MouseInfo mouseInfo = Greenfoot.getMouseInfo(); int x = mouseInfo.getX(); int y = mouseInfo.getY(); getWorld().addObject(new WhatEverClass(), x, y); } This should work.
super_noob super_noob

2012/12/14

#
I'm getting a problem with my private void createBody();
Zamoht Zamoht

2012/12/14

#
How does it look? Please send the code. And if you could send the error as well.
super_noob super_noob

2012/12/14

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

/**
 * Space. The final frontier. 
 * 
 * @author Michael Kšlling
 * @version 1.1
 */
public class Space extends World
{
    private String[] soundFiles = { "bass_guitar1","bass_guitar2","bass_guitar3", "bass_guitar4", "bass_guitar5" };
    
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        super(960, 620, 1);
        
        createObstacles();
        randomBodies(5);
    }   
        
    /**
     * Create a row of abstacles across the middle of our world.
     */
    public void createObstacles()
    {
        int i = 0;
        while (i < soundFiles.length) {
            addObject (new Obstacle (soundFiles[i] + ".wav"), 80 + i*200, i*100+100);
            i++;
        }
        addObject (new Blinker(),Greenfoot.getRandomNumber(940), Greenfoot.getRandomNumber(620));
    }
    
    /**
     * Create a given number of bodies in the universe. Each body has a random initial state (size,
     * mass, direction, speed, color, location).
     */
    public void randomBodies(int number)
    {
        while (number > 0) {
            int size = 20 + Greenfoot.getRandomNumber(30);
            double mass = size * 7.0;
            int direction = Greenfoot.getRandomNumber(360);
            double speed = Greenfoot.getRandomNumber(150) / 100.0;
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            int r =  Greenfoot.getRandomNumber(255);
            int g =  Greenfoot.getRandomNumber(255);
            int b =  Greenfoot.getRandomNumber(255);
            addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y);
            number--;
        }
    }
}
where do I go from here?
danpost danpost

2012/12/14

#
You need to create the code that accepts a mouse click as a trigger to add a new body into the world at the location of the mouse. This code needs to be in a location that can be checked on constantly.
Zamoht Zamoht

2012/12/14

#
I guess you could add my code to the world's act() method like this. public void act() { if (Greenfoot.mouseClicked(null)) { MouseInfo mouseInfo = Greenfoot.getMouseInfo(); int x = mouseInfo.getX(); int y = mouseInfo.getY(); addObject(new WhatEverClass(), x, y); } }
You need to login to post a reply.