Click button to create an object

Whats the easiest way to make a button that when you click it, it will put a box or something on the screen? Sorry if this is a total newbie question or in the wrong forum but I’m learning slowly day by day. Any help would be appreciated.

Thanks,
Andrew [import]uid: 72845 topic_id: 12341 reply_id: 312341[/import]

Hi, fellow newbie) Thats totally basic code for such thing

[lua]local button = display.newRect(100,100,50,50)

local function create_something(event)
if event.phase == “ended” then
local something = display.newCircle(200,200,30)
end
end

button:addEventListener(“touch”, create_something)[/lua] [import]uid: 16142 topic_id: 12341 reply_id: 44948[/import]

Hey fakemode, this type of question should be posted in New Users Start Here - but here is OK too. (Both are appropriate but you’re more likely to get a quick answer in New Users.)

darkconsoles code is spot on - although if you wanted to make it a tad simpler by using tap you could change it slightly to this;

[lua]local button = display.newRect(100,100,50,50)

local function create_something(event)
local something = display.newCircle(200,200,30)
end

button:addEventListener(“tap”, create_something)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 12341 reply_id: 44970[/import]

I love you guys! Is it possible to move the circle freely so when I click the button I can position the circle anywhere I want? If so how would I go about doing that? Also Peach I checked out your site, you rock! Might have to purchase one of your game templates in the future : )

Thanks again,
Andrew [import]uid: 72845 topic_id: 12341 reply_id: 45032[/import]

[lua]local button = display.newRect(100,100,50,50)

local function create_something(event)
if event.phase == “ended” then
local something = display.newCircle(200,200,30)
local function move_something(event)
something.x = event.x
something.y = event.y
end

something:addEventListener(“touch”, move_something)
end
end

button:addEventListener(“touch”, create_something)[/lua]

this works just fine

good luck)

and thats just random funny stuff,try it)
[lua]local physics = require(“physics”)
physics:start()

local button = display.newRect(100,100,50,50)

local bounds = display.newRect(0,0,420,20)
bounds.x = display.contentWidth/2; bounds.y = display.contentHeight
physics.addBody(bounds, “static”, {friction=1,density=1,bounce=1})

local function create_something(event)
if event.phase == “ended” then
local something = display.newCircle(200,200,30)
physics.addBody(something, “dynamic”)
something:applyLinearImpulse(.10)
local function move_something(event)
something.x = event.x
something.y = event.y
end

something:addEventListener(“touch”, move_something)
end
end

button:addEventListener(“touch”, create_something)[/lua] [import]uid: 16142 topic_id: 12341 reply_id: 45077[/import]

looking forward to it, good luck [import]uid: 16142 topic_id: 12341 reply_id: 45105[/import]

You my friend are a saint! Took me a while to get the result I was going for but I finally got it. Is it possible to limit how many circles you can make? Like if make 3 circles the button wont make any more. I Can’t wait to share what I’m creating with you guys.

Thank you so much,
Andrew [import]uid: 72845 topic_id: 12341 reply_id: 45104[/import]

Hey, good stuff!

Yes to your circle question.

Create a variable, say;
[lua]circleCount = 0[/lua]

Then on your button add an if statement, like;

[lua]if cicleCount < 3 then[/lua]

Then when you spawn a circle, add;
[lua]circleCount = circleCount +1[/lua]

Make sense? :slight_smile:

Peach [import]uid: 52491 topic_id: 12341 reply_id: 45191[/import]

Thanks Peach, I tried your code but I’m still getting the same result.

springCount = 0 --YOUR CODE  
local button = display.newImage( "springicon.png" )  
button.x = display.stageWidth / 1.1  
button.y = display.stageHeight / 5.8  
  
 button.active = false  
local function buttonTouched(e) if e.phase == "began" then  
e.target.alpha = .5  
end if e.phase == "ended" then  
e.target.alpha = 1 print("Replay button was touched.")  
 end  
end  
if springCount \< 3 then --YOUR CODE  
  
end  
button:addEventListener( "touch", buttonTouched )  
  
  
local function create\_spring(event)  
 if event.phase == "ended" then  
 local spring = display.newImageRect( "Spring.png",50,50)  
  
springCount = springCount +1 --YOUR CODE  
  
spring.x = 600  
spring.y = 430  
 local function move\_spring(event)  
 spring.x = event.x  
 spring.y = event.y  
 end  
  
 spring:addEventListener("touch", move\_spring)  
  
 physics.addBody( spring, "static", { density=1, friction=0, bounce=1 } )  
 end  
end  
button:addEventListener("touch", create\_spring)  

I’m assuming how I tried implement it is wrong. Sorry I’m such a newb : /
[import]uid: 72845 topic_id: 12341 reply_id: 45376[/import]

Hehe, it’s OK - we’re all newbies at some stage.

Delete this from line 17;
[lua]if springCount < 3 then --YOUR CODE [/lua]

And change line 32 to this;

[lua] if event.phase == “ended” and sprintCount < 3 then [/lua]

That should fix it :slight_smile:

We need to check the springCount immediately before spawning, so you want it there, not way up.

Peach [import]uid: 52491 topic_id: 12341 reply_id: 45410[/import]

Is this what you mean?

springCount = 0  
   
   
local button = display.newImage( "springicon.png" )  
button.x = display.stageWidth / 1.1  
button.y = display.stageHeight / 5.8  
   
 button.active = false  
local function buttonTouched(e) if e.phase == "began" then  
e.target.alpha = .5  
end if e.phase == "ended" then  
e.target.alpha = 1 print("Replay button was touched.")  
 end  
end  
   
   
  
   
   
button:addEventListener( "touch", buttonTouched )  
   
   
   
   
   
   
   
   
local function create\_spring(event)  
 if event.phase == "ended" and sprintCount \< 3 then  
 local spring = display.newImageRect( "Spring.png",50,50)  
   
springCount = springCount +1  
   
spring.x = 600  
spring.y = 430  
 local function move\_spring(event)  
 spring.x = event.x  
 spring.y = event.y  
 end  
  
 spring:addEventListener("touch", move\_spring)  
   
 physics.addBody( spring, "static", { density=1, friction=0, bounce=1 } )  
 end  
end  
button:addEventListener("touch", create\_spring)  

I tried this and when I click the button nothing happens : ( [import]uid: 72845 topic_id: 12341 reply_id: 45481[/import]

Hello
I suggest to you, try to make code cleaner, thats good for you, its easier to navigate that way when code became bigger and complexer

thats the code i wrote and even put some comments in it for learning purposes) good luck, corona is amazing

[lua]_W = display.contentWidth
_H = display.contentHeight
–Our variable that contains number of springs
local springCount = 0
–Our button,that needs to be touched
local button = display.newRect(0,0,50,50)
button.x = _W/2; button.y = _H/2
–Function to move spring, use later in another function
local function move_spring(event)
event.target.x = event.x
event.target.y = event.y
end
–Function to create string at needed x and y; add 1 to springCount per created string
–and add event listener to created string
function create_string()
if springCount < 3 then
local spring = display.newRect(0,0,100,50)
spring.x = 100
spring.y = 100
springCount = springCount + 1
spring:addEventListener(“touch”, move_spring)
elseif springCount == 3 then
print(“stop”) – there you can do anything you want, i just used print for debugging purposes
end
end
–With this function we touch the button and it changes its alpha and use create_string()
local function touch_button(event)
if event.phase == “began” then
print(“began”)
button.alpha = 0.5
elseif event.phase == “ended” then
print(“ended”)
button.alpha = 1
create_string()
end
end
– At last we adding event listener to button and watch magic happens:)
button:addEventListener(“touch”, touch_button)[/lua] [import]uid: 16142 topic_id: 12341 reply_id: 45487[/import]

That was very thoughtful of your, darkconsoles - very clean.

fakemode, is that working for you? :slight_smile: [import]uid: 52491 topic_id: 12341 reply_id: 45588[/import]

Thank you very much Darkconsoles and Peach, this is exactly what I was trying to achieve thank you thank you thank you : ) [import]uid: 72845 topic_id: 12341 reply_id: 45618[/import]