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

2014/10/19

Quick Guidance / Direction for following pathway of dots

1
2
ozcorps ozcorps

2014/10/19

#
So i have looked at the turnTowards and setWorld and intersects... year one java programmer and this seems comprehensible but i just cant get it started i know it will be if in yellow dots then move but seems like manually changing its direction when there has to be a follow colour pathway or something.. thank you in advance for any sort of direction (and yes i did search google for regular searches and also here on the discussion, all have a deep code structure with extra features.. like zombie follow etc.... =))
Super_Hippo Super_Hippo

2014/10/19

#
To follow a pathway of dots, you should create the dots in an array (in the correct order). Then the procedure is the following: (1) - turn to first dot (2) - move (3) - if first dot is reached (4) - increase the index of the array and start again from (1), it's the second dot then
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
The solution given by Hippo really is a nice fast solution, I just want to add that for the
Super_Hippo wrote...
(3) if first dot is reached
you have a few options you can use. That being said some of those options will not be very useful here. Personally I would use a command that only sees the object when you're directly above it. This because otherwise it will just look a bit off. Anyway, I would suggest 'getOneObjectAtOffset'. This command will take the current location of the class it's in (in this case the object that walks from point to point) and then looks for an object at coordinates respective to the coordinates of the object. To clarify: Say your object walking from dot to dot is simply called 'player'. Then within the code of player you could say: turnTowards, then the coordinates of the next dot you get from the array, then move, and then simply add an if-statement like if(getOneObjectAtOffset(0,0, yellowdot.class)!=null). This last command will return 'true' if there is an object of class jellowdot underneath your player. 0,0 simply means the x-coordinates+0 and the y-coordinates+0. yellowdot.class is a specifier. It means getOneObjectAtOffset will ONLY looks for object of the class yellowdot. But anyway, if you don't get my comment Hippo really did a nice job on the overall layout of the code.
Super_Hippo Super_Hippo

2014/10/19

#
I was never sure what the difference between all the touching methods is. If it really only look at its position and the dot is small, you should only do it, if you only walk 1 cell each act-cycle. Otherwise you may never reach it.
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
Well I found kind of a workaround for that with a for-loop. Although my teacher wasn't to happy about it. It should work fine for smaller programs though. I simply used a speed variable which I put into a for-loop. So like this:
int speed = 10;
int counter;

for as far as i get it this makes you change location 10 times in the time it normally takes the program to change your location 1 time. seen you're checking on every change in location mission the dot is gonna be hard...

for(counter=0; counter<speed; counter++)
{
 move 1
 if (getOneObjectAtOffset(0, 0, yellowdot.class)!=null)
 //so if is sees the yellow dot
 //after that you make counter into it's max and make the move-loop false. like this.
counter=speed;
searching=false;
}
Super_Hippo Super_Hippo

2014/10/19

#
Maybe your teacher wasn't happy about this, because you can change the following: -remove line 2 -add 'int' before 'counter=0' in line 6 -line 8: move(1); (- yellowdot → Yellowdot, class names start with a capital letter) -replace line 12 (and 13, what is that?) with 'break;' -line 13 is also not in the if-block. I don't know what you wanted to reach with the searching thing. Oh, and another thing. If you do it like this, you may need to use turnTowards every time, too, because rotation is an Integer. So you may miss the dot, if you start from 10 pixels away.
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
No, he had something about it using a ton of memory. But anyway, it shouldn't be necessary to use turnTowards within the for-loop... I guess... but yeah I get that some things could be shorter and that it's not perfect, but I just like this way of programming better. Keeps it clearer for myself what I'm doing.
Super_Hippo Super_Hippo

2014/10/19

#
I still remember my first Greenfoot project. I also thought that some things aren't necessary. Like it is only interesting that it works somehow. If I look at my project now, I often don't know what I wanted to do in some places. If you start to use a capital letter for class names now, you don't have to change tons of them later. (You can use the ctrl+f (find and replace) feature.) It makes it easier for others to read. At least I always think of objects and not classes, when it starts with small letter. The 'break' is a nice little word that will exit the current loop. And creating useless fields can't be good for keeping it clear. That is one of the good points in for-loops. You don't need to create an extra int like in other loops.
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
I often get a bit defensive when I think people tell me I do a lot wrong even though they are just trying to help. So sorry if my last comment was a bit to much. Then again I did change it to be a bit more appropriate. Obviously you're only trying to help and I really do appreciate that. I will try following your advice a bit more (seen you seem like somebody with a decent amount of experience). Also, I did know about the break; feature, but I don't like using it because I'm still unsure how it works exactly. I know it gets you out of the loop the code is currently in, but that's about it. I guess that's why I tend not to use it but maybe it's worth looking a bit more into it. Anyway, thanks again for the help so far and I hope you can help me out if I get stuck again :). By the way, with making classes into uppercase you also mean the subclasses of World and Actor right?
Super_Hippo Super_Hippo

2014/10/19

#
That's exactly what 'break' does. It leaves the current loop. If you have it in a loop inside a loop, then you don't leave the whole loop, but only the current one. Also you need to end a 'case' inside a 'switch' with a 'break' to prevent the next case running, too. The other thing is 'continue'. With that, you don't exit the loop, you only end the current run. So it will jump to beginning of the loop (in a for loop the index will increase by one). Edit: Yes. (Only the first letter is uppercase.)
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
I was aware of the fact that you need to use break; behind every case you use. So that's not to interesting. However, the thing about 'continue' is new to me, so that's nice to know. Thank you for that. By the way, you could use the break; thing to your advantage, like instead of using many if-statements in one another you can just use a case-statement. You could just make it run 1,2,3,4,5 or 3,4,5 depending on which case is true. Just keep it in mind, might be usefull :). Anyway, thanks again :).
ozcorps ozcorps

2014/10/19

#
Well wow i didnt expect this kind of response ... so first thank you very much... i will look into the above post and give feedback once i get something substantial Thank you again for all this fast and resourceful help ozcorps
ozcorps ozcorps

2014/10/22

#
okay kinda of same task but modified for learning processes of edge collision and boundary....
   if ((leftBumper()==true)&&(rightBumper()==true))
        {
            forward(15);
            turnLeft(90);
            forward(30);
           
        }
so with this on a Left hand turning track using the RIGHT side of the BOX(object) it goes round and round.. btw the track is a square.. hence the 2 bumpers equalling true, i understand why it moves forward to align the BOX(object)" and then turns left and then jumps 30 to align again in its forward position and at a speed of 10 off it goes to the next left hand corner... so walla i feel awesome and learning a great deal.. Now i turn it around and imitate a right hand corner... boy once both Bumpers become true again even though its now on the LEFT side of the BOX(object) it does the LEFT Corner code.... so question is as follows from last... how do i different from this... ps i have standards and that is LOL i think this is mental but its making me think.. left(90) right(90) backward(can be 10 or 20) forward(can be 10 or 15 or 30) thanks again this is exciting stuff and will broaden i guess all thought processes from now on PS dont give me the answer.... i just need someone to give a direction... or something else to look at to im feeling i have to do some sort of greater vs lesser addition using those or more if / else statements.. but alas here i am =)
danpost danpost

2014/10/22

#
Line 4 is, rather, should be, dependent on the direction your actor is traveling.
ozcorps ozcorps

2014/10/26

#
Well just thought id say thank you again for the information and direction.. and currently with the criteria he requested (ie cant change code) there was no answer.. Hence the PROBLEM vs the SOLVING there isnt always a answer was the Lesson.. LOLOLOL.... ahh snap im loving the simplicity and well the life of a Coder etc... Thanks guys and gals
There are more replies on the next page.
1
2