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

2015/4/20

Need help With Jump n' run game (jumping/falling)

1
2
Jax Jax

2015/4/21

#
Super_Hippo wrote...
I already stated it. If 'getOneObjectAtOffset' returns null, it doesn't mean you don't touch the object when you are moving.
Sorry, I didn't see that :) Thank you very much for the solution! :) I rewrote the full code a little bit now. For now it seems that all problems are fixed, will see if there are new ones, once the enemies and other blocks and interactions are there haha. Thanks you very much! - Jax
Jax Jax

2015/4/22

#
Hello :) I got another problem. I created a fireball that is shot when a key is pressed. There is always an error though, when the fireball despawns OR hits the edge of the world and I can't really find out why.... Bounce works perfectly. If it hits 2 walls it gets removed.
1
2
3
4
5
6
7
8
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
    at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
    at greenfoot.Actor.getX(Actor.java:157)
    at Fireball.act(Fireball.java:24)
    at greenfoot.core.Simulation.actActor(Simulation.java:568)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
    at greenfoot.core.Simulation.runContent(Simulation.java:215)
    at greenfoot.core.Simulation.run(Simulation.java:205)
I would really appreciate help! :) Here is the code of the Fireball:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import greenfoot.*;
 
public class Fireball extends Actor
{
    private int ani = 0; //animation variable
    private int timer = 0;
    private int bounce = 0; //bounce counter
    private boolean onGround; //ground variable
     
    public void act()
    {
        if (getX() > 954){ //somehow atWorldEdge didn't work, no clue why so I had to do this
            getWorld().removeObject(this);
        }
        dostuff();
        if (bounce == 0){ //the ball can bounce from a wall once, then if it hits the wall a second time, gets removed.
            bounce();
            setLocation(getX()+6, getY()+4);
        } else if (bounce == 1){
            setLocation(getX()+6, getY()-4);
            bounce();
        } else if (bounce >1){
            getWorld().removeObject(this);
        }
         
         
    }
     
    public void dostuff() { //does stuff
         
        animation();
        if (timer >=85 && timer <90){ //despawn animation, when time runs out
            setImage("fireball8.png");
        } else if (timer >=90 && timer <95){
            setImage("fireball9.png");
        } else if (timer >=95 && timer <100){
            setImage("fireball10.png");
        }
        ani++; //animation variable +1
        timer++; //timer varlable +1
        if (timer >=100){ //after a time, the fireball gets removed
            getWorld().removeObject(this);
        }
    }
     
    public void bounce(){
        onGround = false; // for current on ground state
        Actor ground = getOneIntersectingObject(Ground.class); //checks if in ground of not
        if (ground != null){ //If Fireball is in Ground
            setLocation(getX()-5, getY()-3);
            bounce++;
        }
    }
    
    public void animation(){ //animation for left movement
        if (ani == 0){
            setImage("fireball1.png");
        }
        else if (ani == 8){
            setImage("fireball2.png");
        }
        else if (ani == 16){
            setImage("fireball3.png");
        }
        else if (ani == 24){
            setImage("fireball4.png");
        }
        else if (ani == 32){
            setImage("fireball5.png");
        }
        else if (ani == 40){
            setImage("fireball6.png");
        }
        else if (ani == 48){
            setImage("fireball7.png");
            ani = 16;
        }
    }
   
 
}
danpost danpost

2015/4/22

#
You are giving three reasons to remove a fireball from the world (a) time limit expired; (b) double bounce off walls; and (c) leaving world (edge encountered). (c) is coded at lines 12 and 13; (a) is at the end of the 'doStuff' method which is called immediately after (c) without any restrictions on the call; and (b) is at the end of the act method. Basically if any one removes the object from the world and you then call any methods that require the object be in the world, you will get an error. This includes all methods except those dealing with its rotation and image with a special not on the 'getWorld' method. You can call 'getWorld' on the actor; but, you cannot safely call a method on what it returns without first ensuring that it has not returned a 'null' value. Solving the initial one is easy. Add a 'return;' statement after line 13 so the rest of the method is not executed when it is removed from the world there. The next one happens inside the 'doStuff' method and you cannot so easily just add a 'return' statement to force an exit from the act method on that one. So after line 15 (the call to the 'doStuff' method), add the following line:
1
if (getWorld() == null) return;
which basically say, "if the actor is no longer in the world, exit the act method'. The final one, is at the end of the act cycle for the actor, so there is nothing to avoid there and nothing needs to be done for that one.
Jax Jax

2015/4/23

#
Awesome! :) Thank you very much! Everything is working fine and I think it looks pertty good so far haha Really, thank you very much for the help! :)
Jax Jax

2015/4/24

#
Wow, no clue why I wrote in german there .... o_O Corrected it and thanks again haha Is it btw possible to just change the direction the fireball is flying in? At the moment I use the setLocation method to move the fireball down-right. So I guess that a rotation is only possible with move() ? Would be probably better if the fireball can bounce from every wall and not just from the bottom one (ground) I guess? Thanks!
You need to login to post a reply.
1
2