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

2013/3/11

Problem with getWidth()

sopi sopi

2013/3/11

#
Hey guys, I started to programm a breakout scenario and I got a problem with getWidth(). I want to check whether the ball is hiting a block but if I try to compile I always get the message "cannot find symbol - getWidth()". Do I have to update my programm or what is the problem?
kante162 kante162

2013/3/11

#
Could you post the piece of code where getWidth() ist used, please? I need to take a look at it. But maybe you want to check your code again yourself, maybe you just forgot the brackets :)
sopi sopi

2013/3/11

#
/**
	 * Überprüfen, ob wir auf einen Block treffen.
     */
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        if (block != null) {
			int offset = Math.abs(getX() - block.getX());
			if(offset>=block.getWidth()/2) {
				deltaX = -deltaX;
  			}
			else {
				deltaY = -deltaY;
			}
        }            
    }
kante162 kante162

2013/3/11

#
Okay, i may know the problem. I think you first have to save the picture in a variable, before you can use it.
    /** 
     * Überprüfen, ob wir auf einen Block treffen. 
     */  
    private void checkBlock()  
    {  
        GreenfootImage background; //the variable for the picture
        Actor block = getOneIntersectingObject(Block.class); 
        background = block.getImage();
        if (block != null) {  
            int offset = Math.abs(getX() - block.getX());  
            if(offset>=background.getWidth()/2) {  //changed "block" to "background"
                deltaX = -deltaX;  
            }  
            else {  
                deltaY = -deltaY;  
            }  
        }              
    }
I think this should do it, but i guarantee for nothing. I am pretty new at coding, too :)
sopi sopi

2013/3/11

#
Now I get no error but know the ball doesn't bounce back from the blocks he destroys the blocks but is moving forward instead of bouncing back. And the getWidth method also works when I play games in my internetbrowser it only doesn't works if I try to compile something. Also if I download a game I played in the browser and try to compile it on my PC I get the error, so I don't think the source code is wrong.
danpost danpost

2013/3/11

#
@kante162, line 8 should be inside the major 'if' block (if 'block' is 'null' then you will error on trying to get its image). @sopi, the following should not cause an exception:
private void checkBlock()  
{  
    Actor block = getOneIntersectingObject(Block.class); 
    if (block != null)
    {  
        int offset = Math.abs(getX() - block.getX());  
        if(offset>=block.getImage().getWidth()/2)
        {
            deltaX = -deltaX;  
        }  
        else
        {  
            deltaY = -deltaY;  
        }  
    }              
}
There are, unfortunately, some things that should be accounted for. (1) if your ball travels at a fast rate, it may end up either inside of a block or pass right through a block. (2) the bounce, as you have it now, does not account for the bouncing off a corner properly. Right now, if the ball is to bounce off a corner of the block, it will bounce as if hitting the vertical side of the block. This will make it appear to pass through the block near the ends of the block. (3) there is no code to remove the contacted block (or deal damage to it or score off the hit), which should be done here (not in another class, as having two classes detect the same collision usually does not work; especially if one of the colliding objects is removed from the world).
kante162 kante162

2013/3/11

#
I'm sorry but this task exceeds my capacity :D Also i got a own project to finish.
sopi sopi

2013/3/11

#
OK now I got it. It took a different method for checking the Block without getWidth now it works. But maybe someone could explain me why I always get a error by using the getWidth method.
danpost danpost

2013/3/11

#
To elaborate on (3) above: if the collision is detected in both classes, then there is a strong possibility that either the ball will bounce and the block will not be removed, or the block is removed and the ball will not bounce.
danpost danpost

2013/3/11

#
The 'getWidth' method is a method in the GreenfootImage class documentation; which means you have to use the method on a GreenfootImage object (not on an Actor object). Therefore, you have to get the actor's image to use that method on. The Actor class documentation contains the 'getImage' method which return the actor's image as a GreenfootImage object.
sopi sopi

2013/3/12

#
Ok thank you now I got it. The next and last thing is that I have created three worlds called Board (Where the game is played) Help(Where I show how to play) and Welcome (The welcome screen with which the programm should start where you can select to start the game or read the instruction). So far I got a connection of all three worlds. The problem is the programm always takes the Board world as first world but I want to start with the Welcome World. Is it possible to order the worlds in some ways so that my programm starts with the first one without writing all three worlds in one world?
danpost danpost

2013/3/12

#
Greenfoot will start your scenario with the last manually instantiated world. Right click on the world icon (on the right) that you wish your scenario to start with and select 'new Welcome()' to manually instantiate that world.
davmac davmac

2013/3/13

#
But maybe someone could explain me why I always get a error by using the getWidth method.
getWidth used to be an Actor method in earlier versions of Greenfoot, but in recent versions it is not. You have to call getImage().getWidth() (or equivalent) instead.
You need to login to post a reply.