Prev: Jumping
Next: Vista
From: gtrufitt on
Hi,

I have a character that moves and on RETURN press in a certain location it
runs either a [i]correctanswer[/i] or a [i]wronganswer[/i] script. Here is the
code below, I will try to condense the repeated lines into seprate scripts so
that i can call them and make the code neater but first i need to get it
working. The script runs fine then in question two it does not execute the
RETURN press statements. Any help would be greatly appreciated!

MAIN SCRIPT

global questionnum
global timmy
global lives

on startMovie
set questionnum = 1
set lives = 3


end startMovie

on enterFrame

if questionnum = 1 then
if (_key.keyPressed(RETURN)) then
if (timmy.LocV < 450) then
if (timmy.locH > 59) then
if (timmy.locH < 200) then
correctanswer()
else if (_key.keyPressed(RETURN)) then
if (timmy.locH > 290) then
if (timmy.locH < 390) then
wronganswer()
else if (_key.keyPressed(RETURN)) then
if(timmy.locH > 480) then
if (timmy.locH < 620) then
wronganswer()



else if questionnum = 2 then
if (_key.keyPressed(RETURN)) then
if (timmy.LocV < 450) then
if (timmy.locH > 59) then
if (timmy.locH < 200) then
wronganswer()
else if (_key.keyPressed(RETURN)) then
if (timmy.locH > 290) then
if (timmy.locH < 390) then
wronganswer()
else if (_key.keyPressed(RETURN)) then
if(timmy.locH > 480) then
if (timmy.locH < 620) then
correctanswer()



end if
end if
end if
end if
end if
end if
end if
end if
end if
end if

end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if


end




correctanswer() SCRIPT


global questionnum
global timmy

on correctanswer
questionnum = questionnum + 1
timmy.locv = 470
sprite(13).visible = false
sprite(1).memberNum = 4
sprite(2).memberNum = 5
sprite(3).memberNum = 6
end



wronganswer SCRIPT


global timmy
global lives

on wronganswer
timmy.locV = 470 --beginning loc
sprite(13).visible = true
lives = lives - 1
end






Many Thanks, Gareth

From: Rob Dillon on
Is all of this running as a movie script from the same script window? You don't
really want to be looking for a key event in an enterFrame function. How is the
value of questionnum being set?

You could use a simple behavior to evaluate the location of whatever timmy is
on the Return key event.

How are you separating question 1 from question 2? Are they on different
frames, or are you changing the members of sprites to show different content in
the same frame based on the question number? Or is it something else?

From: gtrufitt on
Many Thanks for your help.

The main script is running within one movie script and the correctanswer and
wronganswer scripts are each running from seperate movie scripts. What function
do you suggest I put the key event within? Question num is being set as a
global variable within the startmovie function to 1. Is this what you mean?

There are sprites containing the questions which relate to members, so the
questionnum will be equal to the member number within a seperate cast library.
The question member sprite changes to the question two member if enter is
pressed within the right section. The same goes for each answer, except the
member will be member(questionnum + 11)etc.

Thanks again, Gareth

From: Rob Dillon on
I reduced your code to this:

-------------------
global questionnum
global timmy
global lives

on startMovie
set questionnum = 1
set lives = 3
the keyDownScript = "checkAnswer"
end startMovie

on checkAnswer
if _key.key = RETURN then
if (timmy.locH < 200) and (timmy.locV < 450) and (timmy.locH > 59) then
correctanswer()
else
wrongAnswer()
end if
end if
end
--------------------------

In the startMovie function a keyDownScript is set. This tells Director to run
a particular function whenever a key is pressed. If this is the only place that
you are looking at keystrokes then this will work fine for you. When a key is
pressed the function checkAnswer will run. It will first look to see what key
was pressed. If it was the Return key then the rest of the code will run.

Since each of your questions is looking at the loc of the same sprite in the
same locations, I'm guessing that timmy is a named sprite, you don't really
care, in this function, which question is being checked.

I'm also guessing that the question can only be right or wrong. So if the
sprite is not in the location to be correct, the answer will be wrong.

This method will only work if you are not using keystrokes anywhere else in
the movie and the sprite timmy is in a frame where this function is called,
otherwise you'll get an error.

A better method is to use a behavior to control the action. That would look
like this:

the behavior that you could attach to the moving sprite:

------------------
property thisSprite

on beginSprite me
thisSprite = me.spriteNum
the keyDownScript = "checkAnswer"
end

on endSprite me
the keyDownScript = ""
end

on gradeAnswer me
if _key.key = RETURN then
if (sprite(thisSprite).locH < 200) and (sprite(thisSprite).locV < 450) and
(sprite(thisSprite).locH > 59) then
correctanswer()
else
wrongAnswer()
end if
end if
end
----------------

and the movie script:

---------------
global questionnum
global timmy
global lives

on startMovie
set questionnum = 1
set lives = 3
end startMovie

on checkAnswer
sendAllSprites("gradeAnswer")
end
-----------------

The behavior will set the keyDownScript at the first frame of the sprite and
remove it at the last frame. The actual keyDownScript is in a movie script
window. This calls back to the sprite's behavior to the function gradeAnswer.



From: McFazstp on
I find it useful to break down complicated if-then statements. As there is
nothing after the if-then block it is a lot easier to work out what's going on.

property p_questions

on beginSprite me
p_questions = [:]

question = [:]
question.addProp( #a, [#left: 59, #right: 200, #correct: TRUE] )
question.addProp( #b, [#left: 290, #right: 390, #correct: FALSE] )
question.addProp( #c, [#left: 480, #right: 620, #correct: FALSE] )

p_questions.addProp( 1, question )

question = [:]
question.addProp( #a, [#left: 59, #right: 200, #correct: FALSE] )
question.addProp( #b, [#left: 290, #right: 390, #correct: FALSE] )
question.addProp( #c, [#left: 480, #right: 620, #correct: TRUE] )

p_questions.addProp( 2, question )

end beginSprite

on enterFrame
if NOT ( _key.keyPressed( RETURN ) ) then
exit
end if

question = p_questions.getAProp( questionnum )

if NOT ilk( question, #propList ) then
exit
end if

repeat with answer in question
if ( timmy.locH > answer[#left] ) AND ( timmy.locH < answer[#right] ) then
if NOT ( answer[#correct] ) then
correctanswer()
else
wronganswer()
end if
exit repeat
end if
end repeat

end enterFrame



 | 
Pages: 1
Prev: Jumping
Next: Vista