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

2012/1/19

removing objects

craigv craigv

2012/1/19

#
Hey all, I want to remove a ball that i shoot out of a cannon once it reaches the outer limits of the world, having troubles getting the right coding for all sides of the world. thanks
craigv craigv

2012/1/19

#
also i get this error message for this code ( if(getY()>=getWorld().getHeight()-1) getWorld().removeObject(this); ) # # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x76ff7adf, pid=1372, tid=4924 # # JRE version: 6.0_26-b03 # Java VM: Java HotSpot(TM) Client VM (20.1-b02 mixed mode windows-x86 ) # Problematic frame: # C # # An error report file with more information is saved as: # E:\Platorms\hs_err_pid1372.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. #
davmac davmac

2012/1/19

#
Probably you're better off posting to separate discussions for different issues, but anyway: for the second issue, that's a Java crash, so your best bet is to upgrade your Java version and then run the "select Greenfoot VM" utility from the start menu (so you run with the newer version). For the first issue: post your code.
craigv craigv

2012/1/19

#
public void act() { move(10.0); if(getY()>=getWorld().getHeight()-1) getWorld().removeObject(this); /*this allows the ball to be removed at the bottom of the world. need help for left and right sides, and top of world.
danpost danpost

2012/1/19

#
You could use
1
2
3
4
5
boolean topEdge = getY() == 0;
boolean leftEdge = getX() == 0;
boolean rightEdge = getX() >= getWorld().getWidth() - 1;
boolean bottomEdge = getY() >= getWorld().getHeight() - 1;
if (topEdge || leftEdge || rightEdge || bottomEdge) getWorld().removeObject(this);
DSP512 DSP512

2014/1/3

#
If I enter
1
2
3
4
5
public boolean atLeftEdge() {
        boolean leftEdge = getX() == 0;
        if(leftEdge) {
            getWorld().removeObject(this);
}
and compile it, I always get the error "missing return statement". If I then type "return;" after the "getWorld().removeObject(this)", I get the error "missing return value". What does that mean?
danpost danpost

2014/1/3

#
DSP512 wrote...
If I enter
1
2
3
4
5
public boolean atLeftEdge() {
        boolean leftEdge = getX() == 0;
        if(leftEdge) {
            getWorld().removeObject(this);
}
and compile it, I always get the error "missing return statement". If I then type "return;" after the "getWorld().removeObject(this)", I get the error "missing return value". What does that mean?
That was not a method that returns a boolean in the previous code. Also, please do not re-activate old discussions. Start a new one stating your problem and showing the pertinent code. The 'boolean' in 'public boolean atLeftEdge() {' says that you will be returning a boolean value from within the method (like 'return true;' or 'return false;' or 'return getX() == 0;'). 'return;', by itself (without a value), can only be used in methods that have a 'void' return type.
DSP512 DSP512

2014/1/3

#
Ok, I found the misstake. The code should look like this:
1
2
3
4
5
6
7
8
9
public boolean objectAtLeftEdge() {
boolean leftEdge = getX() == 0;
if(leftEdge){
return true
}
else {
return false
}
}
And the Act-Method looks then like this:
1
2
3
4
5
6
7
8
9
public void act {
if(objectAtLeftEdge()==true) {
getWorld().removeObject(this);
return;
}
else {
//whatever you want to do instead
}
}
And everything's fine :)
danpost danpost

2014/1/3

#
DSP512 wrote...
Ok, I found the misstake. The code should look like this:
1
2
3
4
5
6
7
8
9
public boolean objectAtLeftEdge() {
boolean leftEdge = getX() == 0;
if(leftEdge){
return true
}
else {
return false
}
}
And the Act-Method looks then like this:
1
2
3
4
5
6
7
8
9
public void act {
if(objectAtLeftEdge()==true) {
getWorld().removeObject(this);
return;
}
else {
//whatever you want to do instead
}
}
And everything's fine :)
Lines 5, 6 and 8 can be removed from the 'act' method, as the return statement performs like an 'else' for the rest of the method. This can all be simplified to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public boolean objectAtLeftEdge() {
    return getX() == 0;
}
// and
public void act()
{
    if (objectAtLeftEdge()) {
        getWorld().removeObject(this);
        return;
    }
    // whatever you want to do instead
}
// if checking all edges, then
public void act()
{
    if (objectAtLeftEdge() || objectAtRightEdge() || objectAtTopEdge() || objectAtBottomEdge()) {
        getWorld().removeObject(this);
        return;
    }
    // whatever you want to do instead
}
DSP512 DSP512

2014/1/4

#
thank you
You need to login to post a reply.