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

2012/12/1

Problem with a controller

zakariaonlyy zakariaonlyy

2012/12/1

#
Hey guys, So i am doing this task for school, which should contain three different objects(Dijk, Water, Gras) on and 8*8 field. Dijk and Gras are called using two different for loops. And for Water i am using a timer and adding the object from there. However i want that when the object, reaches the third Gras Object of the seventh row to print a message saying "Dijk has been broken". I have tried this using the getOneObjectAtoffset(), but it doesnt work. I hope one of you guys can help me out. This is what my code looks like : import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage import java.util.Random; public class WombatWorld extends World { /** * Create a new world with 8x8 cells and * with a cell size of 60x60 pixels */ public WombatWorld() { super(8, 8, 60); //setBackground("cell.jpg"); maakWater(); maakDijk(); maakGras(); } public void populate() { } private long timeStarted = System.currentTimeMillis(); int x=0; int y=3; public void act() { long currentTime = System.currentTimeMillis() ; long elapsedTime = currentTime - timeStarted; if (elapsedTime / 1000 > 0.1) { timeStarted = currentTime; addObject(new Water(),x,y); x++; if (x == 8){ y++; x = 0; } if (getOneObjectAtOffset(8, 8, Water.class)!= null){ System.out.print("Dijkdoorbraak"); //Greenfoot.stop(); } } } public void maakWater() { for(int y = 0; y < 3; y++) { for(int x=0; x < 8; x++) { addObject(new Water(),x,y); } } } public void maakDijk() { for(int y = 3; y < 6; y++) { for(int x=0; x < 8; x++) { addObject(new Dijk(),x,y); } } } public void maakGras() { for(int y = 6; y < 8; y++) { for(int x=0; x < 8; x++) { addObject(new Gras(),x,y); } } } } Hope you can help me out, Greetings, Zakaria
Gevater_Tod4711 Gevater_Tod4711

2012/12/1

#
I'm not sure if I understood your right but if you just want to print the method when you have reached a specific coordinate you could just use "if" to check if you have addet an object on this position like this:
 if (elapsedTime / 1000 > 0.1) {
    timeStarted = currentTime; addObject(new Water(),x,y);
    x++;
    if (x == 8) {
        y++;
        x = 0;
    }
    if (x = targetX && y = targetY) {
        Greenfoot.stop() //or whatever you want to do in this case;
    }
}
zakariaonlyy zakariaonlyy

2012/12/1

#
Yes that is what i want to do, when i reach a specific coordinate i want it to print out a string. When i apply the if statement you wrote up here it tells me, cannot find symbol- variable targetX. It will probably be the same for target y. What does the targetX stand for? And how do i define the specific coordinates?
zakariaonlyy zakariaonlyy

2012/12/1

#
Pardon me for being a noob and thanks for helping out !
Gevater_Tod4711 Gevater_Tod4711

2012/12/1

#
targetX is not declared in your code. It just stands for the xCoordinate you want to reach (targetY is for the y coordinate). If you just delete the targetX and write the x coordinate you want to reach and do the same for targetY it'll work.
zakariaonlyy zakariaonlyy

2012/12/1

#
I understand :D so i have declared int targetX=8; and int targetY=8; When i compile it still tells me that the && is a problem cause it says : bad operand types for binary operators "&&" any ideas? code looks like this : private long timeStarted = System.currentTimeMillis(); int x=0; int y=3; int targetX = 8; int targetY = 8; public void act() { long currentTime = System.currentTimeMillis() ; long elapsedTime = currentTime - timeStarted; if (elapsedTime / 1000 > 0.1) { timeStarted = currentTime; addObject(new Water(),x,y); x++; if (x == 8){ y++; x = 0; } if ( x = targetX && y = targetY) { Greenfoot.stop(); //or whatever you want to do in this case; } //if (getOneObjectAtOffset(8, 8, Water.class)!= null){ //System.out.print("Dijkdoorbraak"); //Greenfoot.stop(); // } } }
danpost danpost

2012/12/1

#
You need to use the equal comparison symbol '==' in the 'if' conditions.
zakariaonlyy zakariaonlyy

2012/12/1

#
Works perfectly ! Thanks for helping out guys!
zakariaonlyy zakariaonlyy

2012/12/5

#
Guys any idea how to store the element Dijk in an array? i know how to create an array, but how do i put an already existing object in an array?
Gevater_Tod4711 Gevater_Tod4711

2012/12/5

#
you have to use a array from the type of the object you want to put in. The rest is all the same like in every array:
Dijk[] array = new Dijk[numberOfElements];

Dijk[anyFieldOfTheArray] = new Dijk(); //or any other object you want to add to this position.
vonmeth vonmeth

2012/12/5

#
I believe this
Dijk[anyFieldOfTheArray] = new Dijk(); //or any other object you want to add to this position. 
should actually be
array[anyFieldOfTheArray] = new Dijk(); //or any other object you want to add to this position. 
Gevater_Tod4711 Gevater_Tod4711

2012/12/6

#
oh right. you have to use the name of the array (in this case array) if you want to get one of the fields. thank you vonmeth.
You need to login to post a reply.