Spawning buttons

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Spawning buttons

Post by dburdan » Tue Apr 12, 2011 2:16 am

OK, so I am trying to spawn 1-10 buttons randomly within a square. here is what I have so far:

Code: Select all

on levelGenerate
   put random(10) into tCount
   repeat tCount times
      create button ("jack" & tCount) in group "alljacks"
      --Set the properties of the "jack"
      set the location of button ("jack" & tCount) to (randomNum(52,266),randomNum(87,383))
   end repeat
end levelGenerate

function randomNum lowerLimit,upperLimit
   return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomNum
So my issue is that it doesn't really place them randomly, it places like 5 on top of each other and it won't spawn any buttons if I try to create them in a group. I also want to be able to delete all the spawned buttons at the end of the round. I figured you would just delete the group but it wont let me create one in the first place. How can I go about this the correct way?

Thanks

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Spawning buttons

Post by Dixie » Tue Apr 12, 2011 4:06 am

Hi...

The attached stack might help you on your way...

be well

Dixie
Attachments
Make&Delete.zip
(1.07 KiB) Downloaded 309 times

dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Re: Spawning buttons

Post by dburdan » Tue Apr 12, 2011 4:30 am

That was everything I needed and more!! Thanks a ton Dixie!!

witeowl
Posts: 45
Joined: Mon Feb 21, 2011 3:02 am

Re: Spawning buttons

Post by witeowl » Tue Apr 12, 2011 5:31 am

For the heck of it, I kept looking at your code, trying to figure out why it wouldn't work right. The problem, it seems, lies in your use of tCount with both "repeat tCount times" and create/locate button ("jack" & tCount). tCount doesn't go down when you loop like that, so each button is named the same thing and therefore each button is placed in the same spot (and moved each subsequent creation).

If you change your code to "repeat with tCount = tCount down to 1", you should have a different result.

dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Re: Spawning buttons

Post by dburdan » Tue Apr 12, 2011 6:02 am

Thanks Whiteowl. That makes sense now! I do have another issue. I can now generate the buttons correctly, but I want the user to be able to click and delete them individually. Breakdown:

• Buttons are generated
• User clicks as many buttons as possible in x amount of seconds
• Once the round is over, all the buttons the user missed are deleted (error here)

That last line is where I'm having my problems. The buttons delete when the user clicks on them but they don't delete at the end of the round. I'm using the same code as provided by Dixie.

Code: Select all

This is the code I use to dlete the button.

on touchStart
        delete the target
//Blah Blah other code here
end touchStart
Any thoughts?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Spawning buttons

Post by Dixie » Tue Apr 12, 2011 9:43 am

Hi...

In that stack that I posted for you there are two buttons... 'Make Buttons' and 'Delete Buttons'. When the buttons are created, the ID's of those buttons are stored in a global property 'theIdsToDelete' (probably be cleaner to store the ID's in a custom property of the card ... I was being lazy ...) So, when your level has finished, you might be left with some buttons that you created that have not been deleted... Amend the script of the 'Delete Buttons' to something like...

Code: Select all

global theIdsToDelete

on mouseUp
   lock screen
   put 1 into count
   repeat for the number of lines of theIdsToDelete
      /* check to see if this button exists */
      if there is a button id (line count of theIdsToDelete) then
         delete button id (line count of theIdsToDelete)
      end if
      add 1 to count
   end repeat
   
   put empty into theIdsToDelete
end mouseUp
The buttons that have not been deleted, will now be deleted...
Hope it helps... be well

Dixie

dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Re: Spawning buttons

Post by dburdan » Tue Apr 12, 2011 5:12 pm

Once again, everything I needed and more! Thanks Dixie!

dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Re: Spawning buttons

Post by dburdan » Wed Apr 13, 2011 12:57 am

Sorry but one last thing! How can I check to see when my last button was deleted? That way I can start a new round.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Spawning buttons

Post by Dixie » Wed Apr 13, 2011 2:54 am

Hey there ... :-)

I have posted you another stack... I have commented the scripts, so I guess... have a look. The buttons are made, you can now delete them by clicking on them as well as just by using the 'delete button'.

Each button now has a script put into it as it is created ... look at this script and the idle script in the card to see how the buttons are deleted and how you will know when to start a new level... (it is probably, well no.. it is better to use a 'send in time' message' especially if you have a lot going on... if it's a bit slow, I'll change the idle handler out.)

take care

Dixie
Attachments
Make&Delete 2.livecode.zip
(1.72 KiB) Downloaded 298 times

dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Re: Spawning buttons

Post by dburdan » Thu Apr 14, 2011 1:22 am

Ok so the code semi-works. It will only run the code on 2nd set of buttons generated. For example, if I only generate one set of buttons, it doesn't work properly. But if I generate 1 set and then another, the second set works. Does that make any sense?

Edit: So it works while running directly in LiveCode but not on iOS.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Spawning buttons

Post by Dixie » Thu Apr 14, 2011 1:42 am

Hi...

Mmm... I'm not sure what to tell you , as it works here. I had not even thought about trying it the simulator before, I had just run the stack on the desktop... but I set it up to run on an 'iPad' in the simulator and all was fine...

regards

Dixie

dburdan
Posts: 104
Joined: Fri Jan 28, 2011 5:39 am

Re: Spawning buttons

Post by dburdan » Sat Apr 16, 2011 8:22 pm

So I got it working by using touchStart and deleting the target. It is now working perfectly. Thanks for all your help Dixie!

Post Reply