Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Recursive methods and collision detection
Old
sam.no21
Guest
 
Status:
Posts: n/a
Default Recursive methods and collision detection - 06-15-2010, 08:15 AM

Some background info before I explain my problem.

In my world I have 3 methods to check how close the moving object (person) is to a section of a large grid. The level 3 method is active when the object is within 100m a certain grid square and rechecks it every 5-6 seconds (to help with lag). If they are closer than 100m it passes up to Level 2 which checks more often and finally to Level 1. In the Level 1 method, if the object is inside the certain grid square, it retrieves the pre-built (during loading) list of objects within that square (not that simple). It runs a different Level 3 method and works its way up to level 1 where it will stop the moving object from bumping into the stationary object.

The problem that I am having is that in the second set of methods, I have If statements to determine if the object is still in the same grid. They look like this:



Basically:
If the object is still in the same grid
Continue with collision detection
Else
Do Nothing


When it reaches the do nothing stage (moving into a different grid), Alice throws:

java.lang.ArrayIndexOutOfBoundsException: 2
at edu.cmu.cs.stage3.alice.core.response.DoInOrder$Ru ntimeDoInOrder.childPrologueIfNecessary(DoInOrder. java:51)
at edu.cmu.cs.stage3.alice.core.response.DoInOrder$Ru ntimeDoInOrder.update(DoInOrder.java:97)
at edu.cmu.cs.stage3.alice.core.response.CallToUserDe finedResponse$RuntimeCallToUserDefinedResponse.upd ate(CallToUserDefinedResponse.java:86)

with heaps more lines and sometimes throws an overflow error.

I have attached my world but when loaded you need to wait for ready to appear in the text console. I will fix this later with a loading bar as well as the grids are visible and the colision barriers are visible.

Really appreciate all help with this.
Attached Files
File Type: a2w final2.a2w (10.37 MB, 28 views)
   
Reply With Quote
Old
Niteshifter
Guest
 
Status:
Posts: n/a
Default 06-16-2010, 01:06 AM

Your 'ArrayIndexOutOfBoundsException: 2' error is coming from the program trying to grab an object from a list/array that doesn't exist (it returns nil, so it's not able to handle it).

Your overflow error simply has something to do with the recursion you have in your program (the method calls itself too many times).
   
Reply With Quote
Old
bedlam
Guest
 
Status:
Posts: n/a
Default 09-09-2010, 05:38 PM

Maybe you need to rethink your routines. If I understand right, you are sort of daisy-chaining your methods together. Your control structure is at the end of your one method, and it is the tail-end of this method that decides what to do next. In some cases, the next thing is to run the method again. The longer the program runs, the longer the chain grows until something chokes.

Unless there are extenuating circumstances, you might consider using a loop in your main method that runs repeatedly until the game is over (ie. the game IS over when you leave the loop). Inside the loop you have lots of "if statements" and other brainy code that check in with different functions and variables to provide it with continuously updated information. When something needs to be done, pawn it off on a method to do. The key is that the method only does work and does not do any deciding on what comes next after it is done it's work. Rather control goes back to that nice loop that can merrily chase it's tail for hours without choking, unlike your daisy-chained recursive methods.

Cheers,

Andrew
   
Reply With Quote
Old
JonsieUSMC
Guest
 
Status:
Posts: n/a
Default 09-11-2010, 09:00 PM

You could also call another method that actually makes your person "Do Nothing" once you want him to do nothing.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2019, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.