Some Help to finish the gameplay

hello everyone :slight_smile:

i am almost done the bigger part of my gameplay, but i have some small problems that I left to other time because i did not know how to solve this, now i need some help to finish this small problems of my gameplay:

1Âș first problem
resolved :slight_smile: thanks ng and info642

2Âș problem
 on the if statement, i create a block, that have limit to 3, and when i create a object, it is counting #block, create and he add + 1, now i cant make when i remove the object i need to #block -1, remove a object from the counting
see the bigger comment on code
on this code have 2 more things to correct:
1Âș run the code you will see it:

  • if you create a block and after click on the ball and after create another ball, will see it, the object that you create before the ball, you can drag it, and the block that you create after you can drag, and i need to stop all draggable objects

the second error:
if i click on the ball i should cant create more block, and i can :confused:

that is all problems that remaining on my test code, i really need to finish it without errors because i need to go to final code, and cant resolve this problems because i dont know how i can make it

 --variables  
  
  
  
 local physics = require "physics"  
 physics.start( true )  
 physics.setDrawMode( "normal" )   
 physics.setGravity( 0,0 )  
  
  
 --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
  
 -- Game   
  
 --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
  
  
 local game = function()  
  
 local block1 = {}  
 local active = 0  
 print(active)  
  
 local ball1 = display.newCircle( 80, 120, 20 )   
 ball1:setFillColor( 0, 255, 0 )   
 physics.addBody(ball1,"kinematic", { density=2, bounce=0.3, radius=25});  
 ball1.x = 250; ball1.y = 200  
  
  
 local spawner = display.newRect( 50, 300, 40, 40 )  
 spawner:setFillColor(150, 0, 0)  
 physics.addBody(spawner, "static", {isSensor = true})  
 spawner:addEventListener("collision", spawner)  
  
  
local function touch(event)  
 if event.phase == "ended" then  
 active = active + 1  
 physics.setGravity( 0,9.8 )  
 ball1.bodyType="dynamic"  
 print(active)  
 end  
 end  
 ball1:addEventListener( "touch", touch )  
  
  
  
  
--Objects and Drag  
  
 local function spawnMyObject (event)  
 if event.phase == "began" then  
  
 --remove function  
 function spawner:collision (event)  
 event.other:removeSelf()  
 end  
  
 --objects and physics  
  
 if #block1 \< 3 then  
 block1[#block1+1] = display.newRect( 50, 200, 50, 50 )  
 block1[#block1]:setFillColor( 255, 255, 255, 100 )  
 physics.addBody(block1[#block1], "kinematic", { density=2, friction=0, bounce=0, shape=barril })  
 block1[#block1].isFixedRotation = true  
  
  
  
 local function startDrag( event )  
 local t = event.target  
 local phase = event.phase  
 if "began" == phase then  
 display.getCurrentStage():setFocus( t )  
 t.isFocus = true  
  
 -- Store initial position  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
  
 -- Stop current motion, if any  
 event.target:setLinearVelocity( 0, 0 )  
 event.target.angularVelocity = 0  
   
 elseif t.isFocus then  
 if "moved" == phase then  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
  
 event.target.bodyType = "dynamic"  
  
  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
  
 -- Switch body type to kinematic, to avoid gravity  
 if ( not event.target.isPlatform ) then  
 event.target.bodyType = "kinematic"  
  
  
  
  
  
 end -- last if  
  
 end-- 2Âș if "move" end  
 end --if begin  
  
 --return true  
  
 end -- startdrag end   
  
 if active == 0 then  
 block1[#block1]:addEventListener("touch", startDrag)  
 else   
 block1[#block1]:removeEventListener("touch", startDrag)  
 end  
  
  
  
  
 end -- if #block end  
  
  
  
 end -- 1Âș if "begin" end   
  
  
 end --spawnMyObject end  
  
  
 if active == 0 then   
 spawner:addEventListener("touch", spawnMyObject)  
 elseif active == 1 then  
 spawner:addEventListener("touch", spawnMyObject)  
 end   
  
  
  
  
  
 end--startgame  
 game()  
  
  


 and have one more problem, that i will post on another day :slight_smile:

Thanks very much people :slight_smile:
[import]uid: 26056 topic_id: 15959 reply_id: 315959[/import]

The first problem seems to be a mismatch of naming and other stuff. Try to intent the code better as well.

ball1.name = "ball"  
  
function onCollision (event)  
  
local type1 = event.object1.objectType  
local type2 = event.object2.objectType  
  
if(type1=="ball" and type2=="star") then  
 --\> Remove star and other related stuff here....  
 event.object2:removeSelf()  
 end  
end  
  
ball1:addEventListener("collision",onCollision)  
  

[import]uid: 81188 topic_id: 15959 reply_id: 59089[/import]

i will test now your code and update the code of example 2, thanks you very much info642
:slight_smile: [import]uid: 26056 topic_id: 15959 reply_id: 59091[/import]

Print out type1 and type2 to the console, they could be mixedup :slight_smile: [import]uid: 81188 topic_id: 15959 reply_id: 59094[/import]

I would attach print (“Object is supposed to be removed”)

Object being the name of whatever it is you think will be removed.

I’ve solved more problems using print than anything else. Also, you could build out a small test file, and only test what you are trying to do.

Also, I think this is just a copy and paste error but on line #1 you are missing an l for the local (I added it)

local block1 = {} local active = 0 print(active) local ball1 = display.newCircle( 80, 120, 20 ) ball1:setFillColor( 0, 255, 0 ) physics.addBody(ball1,"kinematic", { density=2, bounce=0.3, radius=25}); ball1.x = 250; ball1.y = 200 game\_objects:insert( ball1 ) [import]uid: 61600 topic_id: 15959 reply_id: 59095[/import]

Both of codes are no updated, and can test both

error testing first example:

Runtime error: 
/main.lua:29: attempt to index global ‘event’ (a nil value)
stack traceback:
[C]: ?
/Users/barezi6/Desktop/main.lua:29: in main chunk

on the line 29 is:
local type1 = event.object1.objectType
[import]uid: 26056 topic_id: 15959 reply_id: 59103[/import]

Runtime error: 
/main.lua:29: attempt to index global ‘event’ (a nil value)
The things I see you are trying to do is remove the star right? Sure you have other stuff, but I wrote this from scratch and the blue ball falls onto block1 and it disappears.

I don’t know what your code is supposed to be doing, I was confused to be honest with you. Instead, I just broke the piece down to it’s most basic form. You are using name, and some other stuff I ripped out

" if(type1==“ball” and type2==“star”) then" which I can see you are after some filtering for collision of some sort.

Here is my code, it’s really simple and just breaks down the essence. The star gets removed (i guess that’s block1?).

Anyway here is the stripped down version of collision detection and removal.

I’m new to programming in general, (barely 4 months learning), so helping others helps me learn but when it gets very complex I get a big ? mark :slight_smile:

ng

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)

–Make the physics collisions more accurate (default is 8, higher is lower performance
–but better collision detection
physics.setPositionIterations(32)
–shortcut variables
local _W = display.contentWidth
local _H = display.contentHeight
–put the ball in game
local ball1 = display.newCircle (100, 50, 40)
ball1:setFillColor (0,0,255)
ball1.x = _W/2 - 5
ball1.y =90
physics.addBody(ball1, “static”)

–Your block1
local block1 = display.newRect ( _W/2 -40 , _H/2, 80, 80)
physics.addBody(block1 ,“kinematic”)
local function touch(event)
if event.phase == “ended” then
physics.setGravity( 0,9.8 )
ball1.bodyType = “dynamic”

end
end
–function that removes block1
local function collided(event)
if event.phase == “began” then
print(“collided: Star removed”)
block1:removeSelf ( )

end
end

–event listener for block 1, so when collided with it’s removed
block1:addEventListener(“collision”, collided)
–touch event to initiate gravity change
ball1:addEventListener( “touch”, touch )

[/code] [import]uid: 61600 topic_id: 15959 reply_id: 59114[/import]

oh thank you very much ng, it is really simple, I was complicating what is easy:) will update the post with doubts

P.S: i have 1 moth with corona, and i am learning all day, some of easy things, i dont know xD [import]uid: 26056 topic_id: 15959 reply_id: 59118[/import]

So did that help you? I know you have more things going on, but if it didn’t help you let me know. I enjoy working through this stuff :slight_smile:

ng [import]uid: 61600 topic_id: 15959 reply_id: 59128[/import]

It would appear that for the block removal storage thing for #2, I *think* that involves creating a table and storing the data.

Don’t look at me on this one, I am still learning about tables :slight_smile:

Here is a post by Jon Beebe. I think if you look at learningcorona.com there is some other information on tables as well (along with a TON of information).

Here is the link to the tutorial: (maybe you can apply this to object storage or something).


http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/

For your “P.S: i have 1 moth with corona, and i am learning all day, some of easy things, i dont know xD

”

I’ve been there, we all have. You know how long it took me to figure out what I posted above? FOREVER. As long as you can understand the CONCEPT of something, then it’s just getting the right code to do what you want. :slight_smile:

One day, you will have a lot of knowledge and then you can help others as well :slight_smile:

ng [import]uid: 61600 topic_id: 15959 reply_id: 59136[/import]

yeh, i want to have more more and more capacity, not only for me but to help the people, where they need, thank you for all the help ng, i will try making again some new code for my last problems, will try to learn more about tables and work with that, but i tried a lot of things and nothing worked, but will try now with some diferent algorithm with tables. [import]uid: 26056 topic_id: 15959 reply_id: 59176[/import]

Hey guys, Nicholas and Andre,

I am in this boat too! Let`s keep going and sure studying as well! Hope to get better as soon as possible also.

Helping each other truly we are helping ourselves too! :slight_smile:
Regards, [import]uid: 89165 topic_id: 15959 reply_id: 59246[/import]

Hi Rodrigo :slight_smile:
well, i learn a little big more about tables, and i think that the problem isnt the method used to create objects on table, but i really dont know how i can resolve the problems because i dont want to drag a object when i click on the ball, and it happen, try to create a object, and click on the ball, after create another object, and you will see that you can drag the object that you create before click on the ball, and cant drag the object after click on the ball, and i want to cant drag any objects, the after and before created objects, and i really dont know how can resolve it [import]uid: 26056 topic_id: 15959 reply_id: 59545[/import]

Hi andre,
Are you using physics? Did you try to set that object to “static” into its parameters?

Sorry
but I am not sure about the tips. Its just a try!
[import]uid: 89165 topic_id: 15959 reply_id: 59559[/import]

hi rodrigo, yes i use physics but i need to stop all draggable objects, and i only can do it using that “removeeventlistener
” not about body type, so i can stop objects, but only when it is created after i touch on the ball, because when i touch on the ball the variable active is = 1 and when is = 1 i stop the draggable objects, but only stop the objects that was created after i touch on the ball because the objects created before i touch on the ball i can continue dragging and it is wrong, i need to stop all objects, any user with more experience can help me with what i should change etc? i really need to continue my game and need to complete this part of the game because is very important [import]uid: 26056 topic_id: 15959 reply_id: 59620[/import]

Simple modified code, to make a simple function, that i cant do :confused:
i only need to do it:
when the active variable is equal to 1 i remove the event listener, because i want: if active = 0 i can create objects, and if active = 1 i cant create objects, something is wrong, because, if i change tha value of local active to = 1 and it works well, but the default value is 0 and when i click on ball it increment one more value that active = 1, but he create again if touched, can anyone explain how it dont work? because i am learning but cant resolve it, and it is very importante because is used in very other things, and i really want to learn :slight_smile:
thanks

[code]

display.setStatusBar( display.HiddenStatusBar )
local game_objects= display.newGroup()
local physics = require “physics”
physics.start( true )
physics.setDrawMode( “normal” )
physics.setGravity( 0,0 )

–***************************************************

– Game

–***************************************************

local pausegame = function()

local active = 0
print(active)

local ball1 = display.newCircle( 80, 120, 20 )
ball1:setFillColor( 0, 255, 0 )
physics.addBody(ball1,“static”, { density=2, bounce=0.3, radius=25});
ball1.x = 250; ball1.y = 200
game_objects:insert( ball1 )

local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)
game_objects:insert( spawner )

local function touch(event)
if event.phase == “ended” then
active = active + 1
print(active)
physics.setGravity( 0,9.8 )
ball1.bodyType=“dynamic”
end
end
ball1:addEventListener( “touch”, touch )

local block1 = {}

local function spawnMyObject (event)

if event.phase == “ended” then

if #block1 < 3 then
block1[#block1+1] = display.newRect( 50, 50, 50, 50 )
block1[#block1]:setFillColor( 255, 255, 255, 100 )
physics.addBody(block1[#block1], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true

end – if #block end

end – 1Âș if “ended” end

end --spawnMyObject end
if active == 0 then
spawner:addEventListener(“touch”, spawnMyObject)

elseif active == 1 then
spawner:removeEventListener(“touch”, spawnMyObject)
end

end–startgame

pausegame()
return game_objects
[/code] [import]uid: 26056 topic_id: 15959 reply_id: 59635[/import]

Sorry Andre,

Its really hard to me to solve this or get a nice help to give you out with. :\

Let`s wait for the experienced coders.
[import]uid: 89165 topic_id: 15959 reply_id: 59814[/import]