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

2021/5/31

VARIOUS

1
2
3
danpost danpost

2021/6/3

#
Line 38 is limiting the number of Ball06 actors in your world to only one. I know you only to add 12 of them -- but only once. So, change line 17 to:
protected void addedToWorld(World world)
Remove lines 18 thru 23. Now, remove lines 38, 39 and 41. Line 40 (from above) can then be changed to:
world.addObject(new Ball06(), x+100, y+100);
ronald ronald

2021/6/3

#
thank you it works I try to understand the Greenfoot API * GetObjects serves to get other objects from another class or the world * addedtoworld - the method is called when the actor or ANOTHER CLASS is insert or adds in the world or NOT I did not add in the world the class of the ball06, yet it works I try to understand the use of addedtoworld Can you give me more explanation or examples? thank you
Super_Hippo Super_Hippo

2021/6/3

#
‘getObjects’ returns a list of all actors currently in the world which are an instance of the class you pass to the method. An object automatically calls the ‘addedToWorld’ once right after it gets added to a world. Unlike in the constructor which is executed when the object is created, you can for example check for intersection with other actors or distance to other actors in the ‘addedToWorld’ method to prevent an enemy object spawn too close to the player or preventing an apple spawning on the snake in a snake game. In both of these cases, you can simply add the object randomly and in the ‘addedToWorld’ method, you relocate it until it found a valid spot. Another simple example is when you want to save the initial position of the object in the world without passing the x and y coordinates to the constructor.
ronald ronald

2021/6/4

#
okay thank you
danpost danpost

2021/6/4

#
Super_Hippo wrote...
An object automatically calls the ‘addedToWorld’ once right after it gets added to a world.
Actually, I believe the World instance addObject method calls addedToWorld on the actor being added. Another usage is to add some other actor(s) into the world that relate to it -- examples being a player's health bar or a tank's turret.
ronald ronald

2021/6/4

#
thank you danpost and super hippo
ronald ronald

2021/6/5

#
public void paintComponent()
    {
        GreenfootImage image = new GreenfootImage(300, 300);
        GreenfootImage text = new GreenfootImage("Ball01 ", 50, Color.BLUE, Color.BLACK);
        image.drawImage(text, (image.getWidth()-text.getWidth()), (image.getHeight()-text.getHeight())); 
        image.setColor(Color.BLUE);
        image.fillOval(0, 0, 100, 100);
        setImage(image);       
    }
rehello I try to understand drawImage image.getWidth () is 300; text.getWidth is worth how much ??? thank you
danpost danpost

2021/6/5

#
Use the following after line 4 to find out:
System.out.println("text.getWidth is worth: "+text.getWidth());
ronald ronald

2021/6/5

#
good idea I did not think so that I start using System.out.println thank you
ronald ronald

2021/6/8

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

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
        
    public void act() 
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null && Greenfoot.mouseMoved(null))
        {
            int x = mouse.getX();
            int y = mouse.getY();
            getWorld().showText("Mouse is at [" + mouse.getX() + " || " + mouse.getY() + "] ", mouse.getX(), mouse.getY());
            setLocation(mouse.getX(), mouse.getY()-50);
        }
    }    
}
Hello How to make Mouseinfo follow Rocket without the trails? thank you
danpost danpost

2021/6/8

#
ronald wrote...
How to make Mouseinfo follow Rocket without the trails?
Don't you mean "Rocket follow MouseInfo"? And, what do you mean by "trails"?
ronald ronald

2021/6/8

#
as long as Rocket and Mouseinfo are glued or side by side and follow each other everywhere I'm moving Rocket with my mouse, it looks like the smoke or a showText trail but it's not smoke It is full of showtext then it is impossible to read the mouse.getx () and mouse.gety () so much is filled with showtext
danpost danpost

2021/6/8

#
Try:
public void act() 
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse != null && Greenfoot.mouseMoved(null))
    {
        int x = mouse.getX();
        int y = mouse.getY();
        getWorld().showText("", getX(), getY()+50);
        getWorld().showText("Mouse is at ["+x+" || "+y+"] ", x, y);
        setLocation(x, y-50);
    }
}
ronald ronald

2021/6/8

#
Great it works Thank you Danpost
ronald ronald

2021/6/9

#
public void act()
    {
        MouseInfo info = Greenfoot.getMouseInfo();
        if (info != null && Greenfoot.mouseClicked(this))
        {            
            int x1 = info.getX();
            int x2 = info.getX();
            int y1 = info.getY();
            int y2 = info.getY();
            int x = info.getX();
            int y = info.getY();
            
            double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
            
            getWorld().showText("", getX(), getY()+50);
            getWorld().showText("Distance : " + "("+x1+","+y1+") and "+"("+x2+","+y2+")" + distance, x, y);
            setLocation(x, y-100);
        }
    }    
I try to do the same thing but this time it is to calculate the distance between two points (x1, y1) and (x2, y2) clicking After Java, should be used java.awt.point and no x1, x2, xn
There are more replies on the next page.
1
2
3