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

2017/1/7

Interaction MyWorld and actors ?

Lollipoke Lollipoke

2017/1/7

#
Hi ! We want to code a tetris with my sister for fun, but we are a little bit stuck on a point. To make the transition between the piece that has been placed and the next piece, I wrote a little function in MyWorld, with a boolean variable "transition", true when the transition needs to be done and false otherwise. And I want to call my function, nextPiece() in my actor shape. (Since I check the collision between the bottom and my shape, if they are colliding then the piece is placed and the transition needs to be done.) But when I try to call it like "this.getWorld().nextPiece()", Greenfoot doesn't agree haha, and I am not sure how to do it in another way. Here's my colliding function in the shape Actor, and my nextPiece() function, in MyWorld.
if(!(getIntersectingObjects(BottomWall.class).isEmpty())){
            placed = true;
            this.getWorld().nextPlay();
            return true;
        }
public void nextPlay(){
        transition = true;        
    }
//Adds new shape if needed
        if(transition){
            addObject(setNextShape(), (this.getWidth() - leftWall.getImage().getWidth() - rightWall.getImage().getWidth())/2, 0);
            transition = false;
        }
(Sorry if I did any grammar mistakes, english isn't my mother langage).
danpost danpost

2017/1/7

#
Lollipoke wrote...
But when I try to call it like "this.getWorld().nextPiece()", Greenfoot doesn't agree
It is not that Greenfoot does not agree -- the java compiler does not agree. The method 'nextPiece' is not in the World class -- it is in the MyWorld class. The method 'getWorld' returns a World object. It may be an instance of MyWorld, but the compiler does not know that. You can confirm to the compiler that the world will be an instance of MyWorld through typecasting -- a way of telling the compiler what type of World subclass it actually is. Try this: ((MyWorld)this.getWorld()).nextPiece()
You need to login to post a reply.