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

2015/1/7

Actor won't stay on scenario?

1
2
beliuga beliuga

2015/1/7

#
This is the code for my background
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class background2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class background2 extends World
{

    /**
     * Constructor for objects of class background2.
     * 
     */
    public background2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
       // Code for the buttons to stay on the second background. 
     
       super(500, 350, 1); 
    
       addObject(new start2(),318,395);
       addObject(new player1(),165,152); 
       addObject(new player2(),454,149); 
       addObject(new langa(),84,273); 
       addObject(new langb6(),454,149); 
       addObject(new langb56(),454,149); 
       addObject(new langb5(),454,149);  
       
       System.out.println(getObjects(null));
    }
}
However, the objects won't appear on that background. Those objects are buttons and I've put "setLocation(int, int)" in all of their act codes but they still won't show up. Please help!
Super_Hippo Super_Hippo

2015/1/7

#
Do they have a default image or did you set an image for them?
beliuga beliuga

2015/1/8

#
I set an image for them.
danpost danpost

2015/1/8

#
First, right-click on the background of the world and select 'Inspect'. When the pop-up appears, note the title and report back with it. Then, right-click on the background and select from the 'Methods inherited from World' the 'int numberOfObjects()' method. Include the number returned in your next post. BTW, you should not need to include 'setLocation' statements in the act methods of those classes.
beliuga beliuga

2015/1/9

#
Title of the popup : background 1 : background 1 Objects returned : 1 I removed the setLocation after this post. This is weird considering the fact that I put the "addObject" codes into background 2; yet the popup says it's background 1? I think I should mention that there is one actor that is working (my initial start button) but only one.
beliuga beliuga

2015/1/9

#
and even if it says background 1, the objects don't appear on object one when I restart the scenario anyways. Background 1 and Background 2 are separate worlds
danpost danpost

2015/1/9

#
beliuga wrote...
and even if it says background 1, the objects don't appear on object one when I restart the scenario anyways. Background 1 and Background 2 are separate worlds
Re-compile the project; then, right click on the 'Background2' class icon and select 'new Background2()'. Report back what happens and then explain how you want the separate worlds to appear (order, etc.) Also, I would not mind seeing the code to your 'initial start button' actor class.
joanne_wm joanne_wm

2015/1/9

#
AHAHAHAH JOYCE ITS YOU
joanne_wm joanne_wm

2015/1/9

#
are all the actor/animal codes same? If they are, post one up here, including your 2 backgrounds
beliuga beliuga

2015/1/9

#
I get what I did wrong! It's because I wanted to change world, but I accidentally coded it as setImage which didn't change my world so it's still background 1! now I get what I did wrong, what is the code to change the world to the second code?
beliuga beliuga

2015/1/9

#
Okay I've managed to change it to the second world, so now my objects show up. but I somehow can't change the position of some of my buttons, whenever I try to find the int by right clicking, the terminal window comes out and says : java.lang.ClassCastException: start2 cannot be cast to start2
danpost danpost

2015/1/9

#
You will have to copy/paste the entire error message given. Clear the terminal and reproduce the error before copy/pasting into your post.
beliuga beliuga

2015/1/9

#
Okay, so I've managed to fix that button and It is now in place, but I have another problem, I'm trying to add another object into my scenario, but it won't let me compile. the errors that I've got were "(" "{" expected, which I put in the bracket where it told me to, but the second error is telling me that method addObject in class greenfoot.Wolrld cannot be applied to given types required : greenfoot.actor, int int found : story int int
        super(640, 450, 1);
        story storystart = new storystart
        (addObject(storystart,317,383)); 
^ the above was the code I put in, I also put in a set location (although I know you don't need it, but all the buttons I have work and stay in their position only if I put a setlocation in their act code) but It still doesn't work! please help!
beliuga beliuga

2015/1/9

#
and I have no idea why you need a bracket.. so if someone can explain that too because my other buttons work perfectly with the addObject code without brackets?
danpost danpost

2015/1/9

#
Line 3, the 'addObject' line would be fine without the outer pair of brackets. The compiler stopped on that line, and at that place, because the line before it was not completed and it went to line 3 for (hopefully) the rest of that line. You can write lines 2 and 3 as follows:
storystart story = new storystart();
addObject(story, 317, 383);
All method and constructor calls must have brackets after their name whether they are empty or not and a semicolon must follow any executable statement ( 'new storystart()' is a constructor call ). By convention, all classes should begin with an uppercase character and all methods and fields should begin with a lowercase character. With proper convention, the above two lines can be combined like this:
addObject(new Storystart(), 317, 383);
There are more replies on the next page.
1
2