Help with Falling and Collision

Well, the other error is unrelated it seems. Transitions will solve your main scenario, but you’ll need to use various math formulas to make the speed consistent, since arrows may start further away from the center and the time from point A to point B will never be the same. Also, you’ll need to find the math formula to make the arrows point toward the center… that’s very basic trigonometry and there should be a thousand examples online or several in the Corona forums.

Brent

Yeah I will figure that out. But for now, do you think you can help me get started on the main objective? How they fall towards the center object. With the collision details as well… If you could/have time?

Could anyone help me get started with that?

Here is some basic code that will solve the main problem, it has a few problems like spawning objects inside the screen area and the speed not being consistent but will get you started

local halfHeight = display.contentHeight / 2 local halfWidth = display.contentWidth / 2 local transitionSpeed = 2500 local createdObjects = {} local x = 1 local centerObject = display.newCircle( halfWidth, halfHeight, 25 ) centerObject:setFillColor( 0, 1, 0 ) local function change() print("\<- Change to new scene now -\>") end local function createObject() createdObjects.x = display.newCircle( math.random( -300, display.contentWidth + 300), math.random( -300, display.contentHeight + 300), 25 ) createdObjects.x:setFillColor( 1, 0, 0 ) transition.to( createdObjects.x, { time = transitionSpeed, x = halfWidth, y = halfHeight, onComplete = change }) x = x+1 end timer.performWithDelay( 1000, createObject, 0 )

Thanks! But actually that is kind of different than what I had in mind. I was thinking of something pretty different :stuck_out_tongue:

Hi @Spid3rtech,

The community is here to help, and so are myself and Rob, but generally you won’t get somebody to help you write a specific aspect of your game for you. Everything you describe – and why I helped you describe it in much more detail – is easily accomplished with basic transitions from point A to B, and collisions are easily handled if you follow the guide examples (or a sample project) which shows collisions in action.

The guide on creating physics bodies and detecting collisions are here:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

If you prefer to work with actual code instead of reading a guide, we have a sample here:

https://github.com/coronalabs/samples-coronasdk/tree/master/Physics/CollisionDetection

Aside from that, learning these things earlier than later will help you in the long run. Even if somebody writes this code for you, it’ll be best if you learn how to write it yourself. Then, for the next scenario in your game, you’ll have better knowledge of how to approach it.

Take care,

Brent

Thanks for the reply Brent. What if I found the code I erased? If I posted it here or sent it to you by private message, would you be able to help me out? Because I have already read guides. The thing is, it helps me more when someone helps me. Like for instance, when QuizMaster was still active, he helped me code a game, and I learned SO much because of how he helped me. So I think it’s better for me to work side by side with someone. Even if you don’t have time(QuizMaster did not have a lot of time), it still helpes me to work with someone.

Hi @Spid3rtech,

What I would suggest is that you implement one part at a time, and get that fully working, before you move to the next part. So, try to write some code which makes an object “spawn” somewhere outside of the center of the screen, then make it transition to the center using “transition.to()”. That should be possible with just 2 lines of code.

Brent

Wait really?

Ok, I am just putting the code here JUST in case if you want to check it out.

But can you give me an example on that if you don’t mind? To make them move towards the center with transition. I have never learnt about that yet.

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") local physics = require("physics") physics.start() physics.setDrawMode("normal") system.setIdleTimer( false ) function scene:create( event )&nbsp; local BG = display.newImage ("BG.png")&nbsp; local Gem = display.newImageRect ("Gem.png", 50, 50) Gem.x = display.contentCenterX Gem.y = display.contentCenterY local Spear = display.newImageRect ("Spear.png", 15, 50) Spear.x = 100 Spear.y = display.contentCenterY&nbsp; end&nbsp; -- "scene:show()"&nbsp; function scene:show( event )&nbsp; &nbsp; &nbsp; local sceneGroup = self.view&nbsp; &nbsp; &nbsp; local group = display.newGroup() &nbsp; &nbsp; local phase = event.phase&nbsp; &nbsp; &nbsp; if ( phase == "will" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Called when the scene is still off screen (but is about to come on screen).&nbsp; &nbsp; &nbsp; elseif ( phase == "did" ) then&nbsp; -----------------------&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local Spears = {}&nbsp; &nbsp; &nbsp; local SpearsTimer&nbsp; &nbsp; &nbsp; local SpearsCounter = 1 &nbsp; &nbsp; local delayTimer &nbsp; &nbsp; local removeListeners end end local field = display.newCircle( 0, 0, 0 ) ; field.alpha = 0.3 field.name = "field" field.x = display.contentCenterX ; field.y = display.contentCenterY physics.addBody( field, "static", { isSensor=true, radius=320 } ) &nbsp; &nbsp; local spawnSpears = function()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local Fall = math.random(display.contentWidth \* -0.2, display.contentWidth \* 1.2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter].x = Fall&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter].y = -200 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; physics.addBody( Spears[SpearsCounter], "dynamic", {bounce = 0} )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Spears[SpearsCounter].collision = onCollision&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Spears[SpearsCounter]:addEventListener( "collision", Spears[SpearsCounter] )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to( Spears[SpearsCounter], { rotation = Spears[SpearsCounter].rotation+720, time=15000, onComplete=spinImage } )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter].gravityScale = 0.5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sceneGroup:insert(Spears[SpearsCounter]) &nbsp; &nbsp; &nbsp; &nbsp; group:insert(Spears[SpearsCounter]) &nbsp; &nbsp; &nbsp; &nbsp; SpearsCounter = SpearsCounter + 1 &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp;SpearsTimer = timer.performWithDelay( 5000, spawnSpears, -1 ) -- this is a time and 1000 is 1 second and 1000 miliseconds.. it will spawn one circle every one second.. you can change this at your own liking.&nbsp; -- "scene:hide()"&nbsp; function scene:hide( event )&nbsp; &nbsp; &nbsp; local sceneGroup = self.view&nbsp; &nbsp; &nbsp; local phase = event.phase&nbsp; &nbsp; &nbsp; if ( phase == "will" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- composer.removeScene("Game1")&nbsp; &nbsp; &nbsp; elseif ( phase == "did" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --composer.removeScene("Game1")&nbsp; &nbsp; &nbsp; end&nbsp; end&nbsp; -- "scene:destroy()"&nbsp; function scene:destroy( event )&nbsp; &nbsp; &nbsp; local sceneGroup = self.view&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; composer.removeScene("Game") end&nbsp; -------------------------------------------------------------------&nbsp; -- -------------------------------------------------------------------------------&nbsp; -- Listener setup&nbsp; scene:addEventListener( "create", scene )&nbsp; scene:addEventListener( "show", scene )&nbsp; scene:addEventListener( "hide", scene )&nbsp; scene:addEventListener( "destroy", scene )&nbsp; -- -------------------------------------------------------------------------------&nbsp; return scene

Hi @Spid3rtech,

Honestly, my first and foremost suggestion is that you build this outside of Composer, as one simple “main.lua” file. Composer is really a 2nd-level type of thing, and you shouldn’t just dive into that without understanding the foundation of what you want to add to Composer. Only when you have the basics in place should you attempt to place it into the scene management system.

Brent

Actually I do have the basics in place. My main.lua is already filled with my “Beginning” scene and then it “scene:goTo(mynextscene)” and it all works. so bassically, all I need to focus on is this. Think you can help me out by creating a new code? You said it takes like 2 lines of code… for me it took around 10, for just one thing… So if you actually do have time, can you please help me out? If you don’t have time to explain why you did what, I am good at figuring out what you did and how they work and all. I just really want to get this over with xD

OK, well at the very basic level, just transition the “spear” to the center of the screen, or rather the center of the “field” object:

[lua]

transition.to( Spears[SpearsCounter], { x=field.x, y=field.y, time=2000, onComplete=spinImage } )

[/lua]

But then, because the starting point will not be the same distance away for every spear, to accomplish a consistent rate of movement, you’ll need to adjust the time parameter based on the distance. See this post for an example:

https://forums.coronalabs.com/topic/39774-transitionto-with-constant-speed/

Hope this helps you get started,

Brent

Wait, so we aren’t starting over? Or am I adding to the code I have?

Well, you’ve placed some functions and objects in the wrong place(s) within the Composer template, but that can easily be fixed.

On this point, I again urge you to fully understand Composer before you get too much further along, as you will just encounter more problems if you don’t understand exactly how Composer scenes work and what elements should be placed where.

Today we just published a roundup on Composer tutorials and resources. Perhaps these will help you:

https://coronalabs.com/blog/2015/08/11/tutorial-treasury-composer/

Brent, like I said. I like learning from an actual person and actual code. I learn from that better. Links do help, but they do not have the full potential to help me with what I am looking for. For example, If you just made what we are talking about on here, I would probably learn more by just looking at all of the code you inputted then I could ever learn from the link you gave me. Even if you think that could help me, I have wasted a lot of time on links people gave me which did not do anything to help. So I would rather not try and waste my time on that… Sorry. But this post was created because of links.

Hi @Spid3rtech. I had to answer this same question in another thread today.  If you ask a question, then someone, be it Brent, me or a community member has to write an answer and potentially type in some code.

If you ask, then someone else asks a few days later, and some else asks a few days later, we end up answering the same question many times. This is why we write tutorials and ask people to search the forums to see if your question has already been answered. This is why we provide sample apps with the product and to be able to download from github. This is why almost every API has sample code in it’s documentation.  We do this so we don’t have to repeat ourselves over and over.

You tell us you don’t learn as well from links, but if you can read this forum post and read code here, there is no reason you can’t read it in one our tutorials or documentation pages since we would type the exact same answer here.

Please respect our time and the time of our Community members and try better to make use of links provided.  We are giving you the same answer there.

Rob

Well, the point of this post, is because I am looking for something specific. Not a tutorial on how to do Composer, I am pretty sure I get that. What I don’t get is how to do this specific thing.

Hi @Spid3rtech,

I gave you the starting code about 6 posts back, and in addition, a link to another post which contains the code you need for “constant speed”. I could copy the exact same code from that post into here, but you need to go copy it from there and adapt it to your project.

Brent

Actually I’m figuring it out right now. But if I need help I will ask! Thanks for the help so far.