Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Spawning a Limited Amount of Objects — Gideros Forum

Spawning a Limited Amount of Objects

QuasarCreatorQuasarCreator Member
edited June 2012 in General questions
In my game I am working on once I press a button an object is spawned. However, I want to limit the number of times the button can pressed to spawn an object. Here is my code:
	local x=3
	local rec_smallBnt = Button.new(Bitmap.new(Texture.new("images/rec_smallBnt.png")),
	Bitmap.new(Texture.new("images/rec_smallBnt.png")))
	rec_smallBnt:setPosition((application:getContentWidth()- 90), (application:getContentHeight() * 0.5 + 105))
	self:addChild(rec_smallBnt)
 
	rec_smallBnt:addEventListener("click",
		function()
			x = x - 1
			if x == 2 then 
			local rec_small = Bitmap.new(Texture.new("images/rec_small.png"))
			--setting the x and y coordinates randomly
			rec_small:setX(math.random(0, 320 - 100))
			rec_small:setY(math.random(0, 480 - 50))
 
			rec_small.isFocus = false
			--add the event listeners for the two functions
			rec_small:addEventListener(Event.MOUSE_DOWN, onMouseDown, rec_small)
			rec_small:addEventListener(Event.MOUSE_MOVE, onMouseMove, rec_small)
			rec_small:addEventListener(Event.MOUSE_UP, onMouseUp, rec_small)
			--adding the shape to the stage
			self:addChild(rec_small)
 
			elseif x == 1 then
			local rec_small = Bitmap.new(Texture.new("images/rec_small.png"))
			--setting the x and y coordinates randomly
			rec_small:setX(math.random(0, 320 - 100))
			rec_small:setY(math.random(0, 480 - 50))
 
			rec_small.isFocus = false
			--add the event listeners for the two functions
			rec_small:addEventListener(Event.MOUSE_DOWN, onMouseDown, rec_small)
			rec_small:addEventListener(Event.MOUSE_MOVE, onMouseMove, rec_small)
			rec_small:addEventListener(Event.MOUSE_UP, onMouseUp, rec_small)
			--adding the shape to the stage
			self:addChild(rec_small)
 
			end
		end
	)

In this code I am trying to make it so each time the button is clicked x is subtracted 1. Then I am using if and else if statements to spawn only 2 objects which will then call the other functions. However, when I press the button no objects are spawning. I am sure that the way I am doing it is wrong. So, how am I suppose to fix this or how could I do this better?


-Thanks in advance!!

I am a programming noobie, so thanks in advance for helping me out and answering my questions!

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Hmm, it seems strange, but it work ok form. Does it give any errors?
    Here's a little code I've tried:
    local x=3
    local rec_smallBnt = Button.new(Bitmap.new(Texture.new("crate.png")),
    Bitmap.new(Texture.new("crate.png")))
    rec_smallBnt:setPosition((application:getContentWidth()- 90), (application:getContentHeight() * 0.5 + 105))
    stage:addChild(rec_smallBnt)
     
    rec_smallBnt:addEventListener("click",function()
    		x = x - 1
    		if x > 0 then 
    		local rec_small = Bitmap.new(Texture.new("crate.png"))
    		--setting the x and y coordinates randomly
    		rec_small:setX(math.random(0, 320 - 100))
    		rec_small:setY(math.random(0, 480 - 50))
     
    		rec_small.isFocus = false
    		--add the event listeners for the two functions
    		--adding the shape to the stage
    		stage:addChild(rec_small)
    		end
    	end
    )

    Likes: QuasarCreator

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen Thank you! I changed something up and my code started working, however, I also tried your code where x > 0 and it seems to run a little faster (or at least it seemed faster to respond to the second click).
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
Sign In or Register to comment.