on openCard or something else?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

on openCard or something else?

Post by ethanCodes » Sat Mar 05, 2016 6:34 am

Is on openCard the right thing to use for things happening while the card is open? or is it just used for things that happens when the card opens? Is there something else to use "while" the card is open instead of right when it opens? For example, I am trying to let the player click somewhere and then give them 2 options of what to do after clicking. they need to be able to do this as many times as they want. Should I put this code in on openCard or somewhere else?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9824
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: on openCard or something else?

Post by FourthWorld » Sat Mar 05, 2016 7:27 am

Clicking generates mouse messages. Try mouseUp.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: on openCard or something else?

Post by ethanCodes » Sat Mar 05, 2016 8:11 am

Ok. Also, how can I ask the user if he is sure he would like to place an object there or not? Basically, when the user clicks, I want the program to answer by saying "Do you want to place tower?" and give the user the option for yes or no.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: on openCard or something else?

Post by Simon » Sat Mar 05, 2016 8:15 am

Check out "grab" it's cooler.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: on openCard or something else?

Post by dunbarx » Sat Mar 05, 2016 5:13 pm

"openCard" is a single message sent to the card when it opens. Period. It can be used for many things, but once sent, whether trapped or not, the card sits idle waiting for something else to happen. It will wait all day if you let it.

Everything else you do derives from new "local" messages, like "mouseUp" as Richard says.

As for your question, (pseudo)

Code: Select all

answer "Place Tower?" with "yes" or "no"
if it = "yes" then makeAndMoveTower
Now I have no idea how you create of move towers around (in other words. what does "makeAndMoveTower" do). Simon believes you might want to grab and drag. That is fine, and will be fun for you to develop. There are other ways, so write back with your thoughts.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9824
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: on openCard or something else?

Post by FourthWorld » Sat Mar 05, 2016 5:52 pm

Simon wrote:Check out "grab" it's cooler.
Seconded. Mouse messages are the way to go here rather than openCard, for the reasons Craig noted. And Simon's suggestion of using drag and drop is a good one for a nicer user experience: if the user is prompted to choose an action each time they click, it makes for a sort of guessing game ("What will happen when I click here?"), but using drag-and-drop to put an object down lets the user perform the action with intent and confidence.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: on openCard or something else?

Post by ethanCodes » Sun Mar 06, 2016 3:18 am

Alright awesome info guys, thank you! I'm going to try this out and do some experimenting and let you know how it works out!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: on openCard or something else?

Post by ethanCodes » Sun Mar 06, 2016 11:19 pm

So I tried using the grab command and the first tower worked great. the problem is every tower after that. I'm sure it can be fixed with a little code tweaking, I just don't know what needs to be done. Here is what I used:

Code: Select all

on MouseDown
   clone image "Tower"
   grab image "Tower"
end MouseDown
I used clone because otherwise I would just be dragging the one tower around aimlessly. By cloning it, I can make a new one every time I move it. At least that was the plan. The problem is that I move the original, not the clone. Then every time I try to move it a clone drops by the original, which is placed somewhere else on the screen.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: on openCard or something else?

Post by jmburnod » Mon Mar 07, 2016 12:28 am

Hi Ethan,
I have played around your question using mouse move. Are you sure you want use mousedown to send "makeAndMoveTower" message ?
Here is the result :
Btn script (or what you want)

Code: Select all

on mousedown
   makeAndMoveTower
end mousedown
Card script :

Code: Select all

local sMyMoveControlID
on opencard
   put empty into sMyMoveControlID
   put sMyMoveControlID 
end opencard

on makeAndMoveTower
   clone control "tower"
   set the loc of it to the mouseloc
   put "on mouseup;debOneControlMU;end mouseup" into tScript
   replace ";" with cr in tScript
   put the short id of it into sMyMoveControlID
   set the script of control id sMyMoveControlID to tScript
end makeAndMoveTower

on debOneControlMU
      put empty into sMyMoveControlID
end debOneControlMU

on mousemove
   if sMyMoveControlID  <> empty then
      set the loc of control ID sMyMoveControlID to the mouseloc
   end if
end mousemove
Best regards
Jean-Marc
https://alternatic.ch

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: on openCard or something else?

Post by ethanCodes » Mon Mar 07, 2016 12:38 am

I don't understand most of this code....is makeAndMoveTower a user created method? I didn't even know if you could do that in LiveCode or not. I also don't understand most of the code regarding mouseMove....

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9824
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: on openCard or something else?

Post by FourthWorld » Mon Mar 07, 2016 3:09 am

It may be useful to skim some of the User Guide to get a feel for LiveCode scripting.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: on openCard or something else?

Post by Newbie4 » Mon Mar 07, 2016 5:05 am

You have the right idea. All you need to do is save your location, name your clones something else, then move the original back.

Code: Select all

on mouseDown
   grab me
   put the loc of me into myLoc
   clone me
   set the name of me to  "Tower2"
   set the loc of image "Tower" to myLoc
end mouseDown
There may be other ways to do it but this seemed the easiest based on the way you started doing it.

Do you understand what each line does and why that line is needed? Look up each command. Make this a learning exercise
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: on openCard or something else?

Post by ethanCodes » Sun Mar 13, 2016 8:06 am

wouldn't that only work on the first tower dropped? after that they will not be able to increment from tower2 to tower3 and so on, so it will just screw it all up from there. What about this? I'm getting an error but I think if I can fix whatever is causing the error it should work.

Code: Select all

on MouseDown
   clone image "Tower"  at location 679, 97
   grab image "Tower" --move the tower to desired location. drop when mouse released.
end MouseDown

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: on openCard or something else?

Post by richmond62 » Sun Mar 13, 2016 9:21 am

After you have cloned your 'Tower" it might be a good idea to rename it so
you don't end up with lots of objects on your card with the same name:

on mouseUp
clone image "Tower"
set the name of last image to "Tower1"
end mouseUp


Obviously it might be a good idea to keep count by storing numbers in a field:

on mouseUp
clone image "Tower"
add 1 to field "KeepCount"
put "Tower" && fld "KeepCount" into NUNAME
set the name of last image to NUNAME
set the left of image NUNAME to (fld "KeepCount" * 40)
end mouseUp

You will note that I have also added a line ["set the left of . . . ."] to stop the cloned objects piling up on top of each other.
Cloner.png
Cloner.png (8.56 KiB) Viewed 7789 times
Attachments
Cloner.livecode.zip
Here's my demo stack in case you want to play around with it.
(2.13 KiB) Downloaded 192 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”