Page 1 of 3
First Game (help needed)
Posted: Sat Apr 26, 2014 11:38 pm
by catalinanesia
Hi everyone, I am new to programming and LiveCode (very happy with my purchase) so I ended up asking for help obviously after running around my tale for a few days and watching the online Academies.
Here is the scenario of my first game attempt: a Princess is travel on a grass path to collect precious gems. She start travel only after the press of Play button if she collide with a direction "Arrow".
For travel direction I have 4 Arrow buttons (Right, Left, Up, Down). All the graphic in my game are "button's" for some reason I am not using "image".
This is the problem I am facing right now: the Play button runs the flowing script after is pressed
Code: Select all
on mouseUp
if intersect(button "Princess" , button "Right", "pixels" ) then
move button "Princess" to the loc of button "Block2" in 3 seconds
put true into got1
hide button "Gem1"
disable button "Gem1"
else
if intersect(button "Princess" , button "Up", "pixels" ) then
move button "Princess" to the loc of button "Block3" in 2 seconds
hide button "Gem2"
disable button "Gem2"
end if
end if
end mouseUp
What I want is: After press play Princess is travelling to "Block2" and if on "Block2" she is intersecting with "Up" she should continue travel upward without stopping.
Currently my Princess starts travel to "Block2" and it stops there, she is "intersect" with the button "Up" but does not move upward. If "Play" is pressed again
she starts moving "Up"-ward. How can I make it to travel with no stop if she is intersecting the direction arrows ?
Appreciate all the help and guidance I can get !
Catalin
Re: First Game (help needed)
Posted: Sun Apr 27, 2014 5:36 am
by Simon
Hi Catalin,
Your mouseDown script in the start button only runs through it's rules once.
You need to repeatedly check the conditions over time as she moves around.
See in the code below I use a "send" to repeat "moveOn"?
Code: Select all
on mouseUp
moveOn
end mouseUp
on moveOn
if intersect(button "Princess" , button "Right", "pixels" ) then
move button "Princess" to the loc of button "Block2" in 3 seconds
put true into got1
hide button "Gem1"
disable button "Gem1"
else
if intersect(button "Princess" , button "Up", "pixels" ) then
move button "Princess" to the loc of button "Block3" in 2 seconds
hide button "Gem2"
disable button "Gem2"
end if
else
if intersect(button "Princess" , button "Block3", "pixels" ) then --Stop Checking
exit moveOn
end if
end if
wait 0 millisec with messages
send "moveOn" to me in 20 millisecs --Check again in 20 milliseconds
end moveOn
"if intersect(button "Princess" , button "Block3", "pixels" ) then --Stop Checking"
is important, otherwise the loop will continue forever.
Does that help?
Simon
Re: First Game (help needed)
Posted: Sun Apr 27, 2014 6:05 am
by catalinanesia
Greatly appreciated @Simon ,
this worked as a charm, will you correct me if I am wrong please:
You created a handler(I think is called handler) "moveOn" and then when you call mouseUp the code
inside the handler is executed ?
What is a good practice to create handlers library? should they be defined in MainStak? or on Card ? or ...
Thanks,
Catalin
Re: First Game (help needed)
Posted: Sun Apr 27, 2014 6:41 am
by Simon
Hi Catalin,
Yes, it is called a "Handler".
Where you put them depends on where you need them. I believe in this case you will probably want to put it in the stack script, that way you can call it from anywhere.
But I think you are going to have to alter your code to be more efficient as you can see that code will get very long depending on how many places shes has to go.
Simon
Re: First Game (help needed)
Posted: Sun Apr 27, 2014 7:09 am
by catalinanesia
@Simon , Thanks for the hint. I am not worried for the length of code or optimisation at this time.
Just want to do things step by step and to fix them in my memory, later on when I have enough experience
I will go deeper in optimisation, for now I need the basic concept of how LC and programming works to be
clear in my head ...
This journey with Princess will make me learn what and how to use basic concepts (Handler, Variables, Loops, etc.)
Regards!
PS is 2am and I am thrilled to have my Princess adding score now if intersect Gem, etc.
Re: First Game (help needed)
Posted: Tue May 06, 2014 7:04 am
by catalinanesia
I am stuck again, here is my question: how can I trigger an action (handler) with NO mouseUP or keyPressed ?
The Princess is travelling from point A to point B after the Start button is pressed, and in her way she is "intersect"
button Gem1 (at this point I want the Gem1 to "hide"). If I put the script in the Start button it is ok but how I make it work
if I put the script in the Gem1 button ?
Here is the code in Gem1 button which do not work
Code: Select all
on gCollide
if intersect(button "Princess" , button "Gem1", "pixels" ) then
hide button "Gem1"
disable button "Gem1"
--set the visible of the button "Gem1" to false
end if
end gCollide
One more question: what is the principle of trigger events (scripts) not using keyboard and/or mouse input ?
example: 1) if button1 intersect button2 then hide button3
2) if position of button1 = x,y then move button3
NOTES: I noticed that we can put many line of code in one button (Start) and will be triggered by mouseUp or Down handlers
therefore executing all those script lines. I found a bit hard to follow my code after more than 100 lines of code, therefore
I would like to put bits of scripts on the "buttons" rather than all the code in one button. So now I have no idea how to trigger
the scripts by not using mouseUp or keyboard input.
Any help or guidance is welcomed, thanks!
Re: First Game (help needed)
Posted: Tue May 06, 2014 8:42 am
by sefrojones
Catalin,
You're script above works, I suspect you are not checking for the intersection enough. How often Do you call gCollide? Check out this stack I made using your script.
1. drag the princess button over to the gem1 button. (nothing happens)
2. hilite the check button
3. drag the princess button over to the gem1 button again. (it disappears)
4. check out the card script to see how I accomplished this.
hint: you need to check for intersection VERY frequently
Good Luck,
Sefro
Re: First Game (help needed)
Posted: Tue May 06, 2014 8:58 am
by sefrojones
If you haven't already, you should check out this great site with a simple livecode game making course:
https://sites.google.com/a/pgcps.org/livecode/home
--Sefro
Re: First Game (help needed)
Posted: Tue May 06, 2014 11:22 am
by jmburnod
Hi All,
I played with the Sefro's stack and it works fine for me
Except if the hilite of btn "Check" is true when you open the stack
That is strange for me
I try to solve it with a quick and dirty way on opencard
Here is a stack works with several GEm button
Best regards
Jean-Marc
Re: First Game (help needed)
Posted: Tue May 06, 2014 5:41 pm
by catalinanesia
Thanks for the help!
And yes one of the sites I dig in constantly is Mr. Cyril Pruszko one
you posted the link, also that website influenced my decision to buy LiveCode.
I can wait to test the stack later on today "I am at the boring job right now

"
Regards,
Catalin
Re: First Game (help needed)
Posted: Tue May 06, 2014 7:09 pm
by sefrojones
jmburnod wrote:Hi All,
I
Except if the hilite of btn "Check" is true when you open the stack
Good catch. It was getting pretty late, I kind of just threw that stack together real quick before bed. I just updated it so the check box in unchecked at launch.
Catalina,
check out the first lesson of the Game Academy. It covers game loops, and while they use a fancy updatescreen handler, as you can see from the examples above, you don't necessarily need to use theirs, just to understand the concept.
Sefro
Re: First Game (help needed)
Posted: Tue May 06, 2014 7:23 pm
by sefrojones
jmburnod wrote:Hi All,
I played with the Sefro's stack and it works fine for me
Except if the hilite of btn "Check" is true when you open the stack
That is strange for me
I try to solve it with a quick and dirty way on opencard
Here is a stack works with several GEm button
Best regards
Jean-Marc
I just checked out your stack, and i have a question. Is there an advantage to using this method over something like this?
Code: Select all
on gCollide
repeat with x=1 to 3
if intersect(btn "princess",btn ("gem"&x), "pixels") then
hide btn ("gem"&x)
end repeat
end gCollide
I attached a stack using this method. Is there a reason I should be doing it your way?
Thanks,
Sefro
Re: First Game (help needed)
Posted: Tue May 06, 2014 10:32 pm
by jmburnod
Hi Sefro,
Is there an advantage to using this method over something like this?
Using a function is very useful to get the intersected controls
In fact this function would better with parameter and should be in the stack script.
You can also use "control" instead "button" and it works with all types of controls
With this way we can call it from all scripts of the stack for others games that need an intersect result
with others objects
card script:
Code: Select all
local sObjNoMove
on preopencard
set the hilited of btn "check" to false -- don't work if the hilite is true by default (?)
end preopencard
on opencard
initLocals
debResetMU
end opencard
on initLocals
put "Gem1,Gem2,Gem3" into sObjNoMove
end initLocals
on debResetMU
repeat for each item tObj in sObjNoMove
enable control tObj
show control tObj
end repeat
set the loc of control "princess" to 85,319
end debResetMU
on gCollide
get getAllIntersect("Princess",sObjNoMove)
if it <> empty then
hide control it
disable control it
end if
if the hilited of btn "check" is true then send "gcollide" to this cd in 2 ticks
end gCollide
stack script
Code: Select all
function getAllIntersect pObjMove,pObjNoMove
repeat for each item tObj in pObjNoMove
if intersect(control pObjMove , control tObj, "pixels" ) then
return tObj
end if
end repeat
end getAllIntersect
Best regards
Jean-Marc
Re: First Game (help needed)
Posted: Wed May 07, 2014 4:17 pm
by catalinanesia
Thank you! both for the provided help, stack and code.
For the moment I can understand something from Sefro code, it is more simple for me.
Jean-Marc, your code looks very impressive to me and smart
Meanwhile I am reading about Game LOOP and Variables, I have the Start window
of the Princess game and the 1'st Level card (scripts in process)
Re: First Game (help needed)
Posted: Wed May 07, 2014 6:20 pm
by jmburnod
Hi Catalina
Thanks for the flowers.
I hope this script helps you to understand interest of functions and parameters
That is very useful for your project
That is also a good exercice to invent names that make sense to get a good lisibility of your script and easier to test
For exemple I have to change the name of the parameter pObjNoMove to pTargetObj that is clearer.
Long life to the princess
Best regards
Jean-Marc