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

2021/8/19

getWidth and getHeight don't work

alex_rimbu alex_rimbu

2021/8/19

#
can someone please tell me what I'm doing wrong? it says "undeclared method" when I try to implement getWidth and getHeight import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class fizzingBubbles extends Actor { GreenfootImage image = getImage(); int size = 30; public fizzingBubbles() { image = getImage(); image.scale(size,size); } public void act() { int fizzingBubble = Greenfoot.getRandomNumber(50); if (fizzingBubble==4) { int x = Greenfoot.getRandomNumber(10)+getWidth()-50; addObject(new fizzingBubble(), x, getHeight()); } } }
danpost danpost

2021/8/19

#
Only things that use "getWidth()" and "getHeight()" provided by greenfoot are for GreenfootImage and World objects. Your "int x = ..." line tries to use one on an Actor object (of type fizzingBubbles, via implicit use of "this.") and your "addObject(..." line tries to use one on a primitive int variable ("x"), which is not truly considered an object. Try "getWorld().getWidth()" and "getWorld().getHeight()", which uses them on your World object.
alex_rimbu alex_rimbu

2021/8/20

#
Thank you for your help! That solved that error, however, although nothing is underlined in red anymore, it still says "Error(s) found in class." at the bottom of the screen and won't tell me what it is. Do you know what the problem might be?
danpost danpost

2021/8/20

#
In the addObject line, try changing: new fizzingBubble() to: new fizzingBubbles() However, it is recommended that you add these into the world thru your MyWorld class. General rule is that you do not create objects of a class from within its own class. (note that you can have an act method in your MyWorld class).
alex_rimbu alex_rimbu

2021/8/20

#
Oh, got it, thank you! My image still won't show up though. Am I doing something else wrong?
danpost danpost

2021/8/21

#
alex_rimbu wrote...
Oh, got it, thank you! My image still won't show up though. Am I doing something else wrong?
Try:
setImage(image);
after scaling.
alex_rimbu alex_rimbu

2021/8/21

#
The image still does not show up. Is there something I need to add to the constructor in the world class?
Super_Hippo Super_Hippo

2021/8/21

#
Show your current code. Especially how you add the Actor to your world now.
alex_rimbu alex_rimbu

2021/8/21

#
This is how I am adding my actor import greenfoot.*; public class fizzingBubbles extends Actor { GreenfootImage image = getImage(); public fizzingBubbles() { image = getImage(); image.scale(50,50); setImage(image); } public void act() { int fizzingBubble = 4; if (fizzingBubble==4) { //int x = Greenfoot.getRandomNumber(10)+getWorld().getWidth()-50; getWorld().addObject(new fizzingBubbles(), 100, 200); } } }
Super_Hippo Super_Hippo

2021/8/21

#
Re-read this one:
danpost wrote...
However, it is recommended that you add these into the world thru your MyWorld class. General rule is that you do not create objects of a class from within its own class. (note that you can have an act method in your MyWorld class).
You need to login to post a reply.