This site requires JavaScript, please enable it in your browser!
Greenfoot back
rachpaguia@gmail.com
rachpaguia@gmail.com wrote ...

2017/3/14

APPEARING OBJECT

1
2
danpost danpost

2017/3/14

#
Okay, clean-up time:
import greenfoot.*;

public class A extends Actor
{
    private int timer = Greenfoot.getRandomNumber(100)+100; // value to be maintained throughout the life of the actor
    
    public A() // constructor for class objects
    { // stuff that only needs done once while actor is not yet in world
        GreenfootImage image = getImage();
        image.scale(330, 150);
    }
    
    public void act()
    { // actions performed while in world
        setLocation (getX(), getY()+7); // moving
        timer--; // running timer
        if (timer == 0) // spawning C.java object
        {
            addObject(new C.java(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); // adds a C.java object
            timer = Greenfoot.getRandomNumber(100)+100; // resets timer
        }
        if (Greenfoot.isKeyDown("a")) getWorld().removeObject(this); // removing this actor on 'a' press
    }
}
Thank you so much, i'm sorry i've been so messy huhu. Thank you, thank you!
 import greenfoot.*;
 
      public class A extends Actor
btw, these two are seen as errors "illegal start of an expression" the import has a white background and the public class etc is in the green part
danpost danpost

2017/3/14

#
I am using an old version of greenfoot (gf-2.3.0 USB standalone) which I like very much. If there was no import statement in your class before, you then just need to remove the first line.
danpost danpost

2017/3/14

#
danpost wrote...
If there was no import statement in your class before, you then just need to remove the first line.
No. That cannot be it. Try removing all whitespace between the lines (between ';' and 'public') and then pressing enter a couple times.
Super_Hippo Super_Hippo

2017/3/14

#
I think he added the import and the class A into his class, so it is import - start of class - another import - another start of class right now. As I already pointed in my last post, this should be in the world subclass and not in an actor (or line 19 needs several changes). Btw, I don't know if 'C.java()' is correct there.
danpost danpost

2017/3/14

#
Super_Hippo wrote...
As I already pointed in my last post, this should be in the world subclass and not in an actor (or line 19 needs several changes). Btw, I don't know if 'C.java()' is correct there.
You are correct about line 19, it would need to be:
getWorld().addObject(new C.java(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
I was wondering about 'C.java' myself. After some thought on it, I do not think it can be correct -- not 'new C.java()'. 'C.java()' might be used to call a static method called 'java' in the C class and 'C.java' might refer to a static field. However, I cannot think of anywhere where you might use the particular coding of 'new C.java()'. If 'java' was an inner class of the C class, then you would need a C object to create a java object (you would not be able to create one from the class itself). It the inner class was abstract and accessible via the class name, then you would not be able to instantiate one. The only reason I can see why that was used is because the filename of the class has the '.java' extension. However, that is not used when instantiating a class. Probably what was meant was 'new C()'.
I used C.java because I wanted to add an actor that has a name of "c". is it not correct?
/**
 * Write a description of class A here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class A extends Actor
{
    /**
     * Act - do whatever the A wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
      public class A extends Actor
   {
    private int timer = Greenfoot.getRandomNumber(100)+100; // value to be maintained throughout the life of the actor
     
    public A() // constructor for class objects
    { // stuff that only needs done once while actor is not yet in world
        GreenfootImage image = getImage();
        image.scale(330, 150);
    }
     
    public void act()
    { // actions performed while in world
        setLocation (getX(), getY()+7); // moving
        timer--; // running timer
        if (timer == 0) // spawning C.java object
        {
            addObject(new C.java(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); // adds a C.java object
            timer = Greenfoot.getRandomNumber(100)+100; // resets timer
        }
        if (Greenfoot.isKeyDown("a")) getWorld().removeObject(this); // removing this actor on 'a' press
    }
   }
  }
}
the public class extends etc is still an error
danpost danpost

2017/3/15

#
rachpaguia@gmail.com wrote...
I used C.java because I wanted to add an actor that has a name of "c". is it not correct?
That is not correct. Just use 'C'.
the public class extends etc is still an error
Remove lines 9 through 17.
thank u! there is no error found anymore in that part, but there is for the getWidth and height. how come this happened when there was no error earlier?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class A here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class A extends Actor
{
    private int timer = Greenfoot.getRandomNumber(100)+100; // value to be maintained throughout the life of the actor
      
    public A() // constructor for class objects
    { // stuff that only needs done once while actor is not yet in world
        GreenfootImage image = getImage();
        image.scale(330, 150);
    }
      
    public void act()
    { // actions performed while in world
        setLocation (getX(), getY()+7); // moving
        timer--; // running timer
        if (timer == 0) // spawning C.java object
        {
            addObject(new C (), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); // adds a C object
            timer = Greenfoot.getRandomNumber(100)+100; // resets timer 
        }
        if (Greenfoot.isKeyDown("a")) getWorld().removeObject(this); // removing this actor on 'a' press
    }
  }
  

 
Super_Hippo Super_Hippo

2017/3/15

#
This was already answered:
getWorld().addObject(new C(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
Oh, thank you, i'm sorry i wasn't able to see. Thanks again!
Last, sorry, What do spawning objects mean?
danpost danpost

2017/3/15

#
rachpaguia@gmail.com wrote...
Last, sorry, What do spawning objects mean?
To spawn an object is basically the same as to add one into a world.
You need to login to post a reply.
1
2