Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list questions

Status
Not open for further replies.

eladi

Programmer
Sep 4, 2001
80
AU
hi all,

so this is the first time i really try to work with those list in director. i'm trying to build an invaders game. everything fine so far.

no, i have a list with the spirtenumbers of the invaders in it. like this:

pPossibleTargets = [3,4,5,6,7,8,9,10]

then i want to check with the bullet script, if one of the invaders is hit, with the following script:

repeat with i in pPossibleTargets

if sprite(the currentSpriteNum).intersects(i) then
hit sprite(i)
pPossibleTargets.deleteOne(i)
put pPossibleTargets

if pPossibleTargets = [] then
end if

pShooting = false
sprite(the currentSpriteNum).locV = -50
end if
end repeat

when the first shot hits an invader, the script works. the handler hit is excecuted and the listentry is deleted. when the secondtime the bullet hits though, i get the follwing error message:

script error: list expectet for handler
repeat with i in pPossibleTargets
#getAt

i'm confident that somebody can help me out with this problem.

tnx in advance
adrian

 
The deleteOne function isn't the function you want to use for that. Delete one is if you have a list like this:

days = ["Tuesday", "Wednesday", "Friday"]

And you wanna delete the item whoes content is "Wednesday"

days.deleteOne("Wednesday")

Instead, I would use:

pPossibleTargets.deleteAt(i)

This deletes the item at that position in the list, which is what you want. Not sure if this is what's causing the problem, but its worth a shot and it's easy to change. If this doesnt fix it, make sure your list variable pPossibleTargets isnt getting set as void or some other value not of the list type.
 
hei fugigoose,

tnx for your try. this didn't help though. first i had to rearange the script (because the list entries are sprite channel numbers, which i need to delete the hit invaders). after all this, i had still the same error. but i found the solution in the mm director forum:

You'd think deleting elements from a list was a trivial thing to do... and
mostly it is. But here is something that blew my mind:

if listP(test_poofles) then
repeat with poofle in test_poofles
if poofle.is_done() then
test_poofles.deleteOne(poofle)
end if
end repeat
end if

---------------------
the above, gave me errors (usually for the last poofle to be removed) to
the tune of: "error list expected for handler": #getAt in the line:
--> repeat with poofle in test_poofles
---------------------

Below, I do a two phased approach. Instead of deleting items from the list
I'm running through, I first put all the deletions into a new list
(nuke_em), and then delete all of the items from the test_poofles list after
I'm through with them. This works, and never gives me an error.

I see how if deleteOne and the repeat loop where not implemented careful
enough internally (by macromedia), that there would be issues in the above
operation. I'm pretty amazed that there are issues like this though.

if listP(test_poofles) then
nuke_em = []
repeat with poofle in test_poofles
if poofle.is_done() then
nuke_em.add(poofle)
end if
end repeat

repeat with poofle in nuke_em
test_poofles.deleteOne(poofle)
end repeat
nuke_em = []
end if

----------------------------------------------------------

There are two things you can do --

(1) count backwards down the list (that way, if you delete an object from
the end of the list it doesn't screw up the list index). eg

mx = aList.count
repeat with i = mx down to 1
if someCondition then aList.deleteAt(i)
end repeat

(2) use a duplicate of the list eg.

copyList = originalList.duplicate()
repeat with anObject in copyList
if anObject.isDone() then
-- delete from the original list
originalList.deleteOne(anObject)
end if
end repeat

Incidently, this approach is very handy if you are calling objects in a list
or using step frame, and the object wants to delete itself from the list. As
soon as the object deletes itself from the list, Director will notice that
the list has changed and abort the call or stepframe event meaning that any
objects after the object that deleted itself will not receive the original
call or #Stepframe message. One way around this problem is to call a
duplicate list - eg.

call(#checkIfDone, test_poofles.duplicate())

Then the objects in the duplicate list can safely delete themselves from the
original list without preventing the call from being passed on to subsequent
objects in the list.

Luke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top