Hi guys,
I really need help.
So I have a circle(on picture) with a 6 colors and I want to make that balls can have collision with that circle ( like red ball with red part on circle) and after collision it removes and increase score by 1 or 2.
Here is my code:
require "ssk.loadSSK" -- -- Set up physics local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) -- -- Forward Declarations -- SSK local angle2Vector = ssk.math2d.angle2Vector local vector2Angle = ssk.math2d.vector2Angle local scaleVec = ssk.math2d.scale local addVec = ssk.math2d.add local subVec = ssk.math2d.sub local normVec = ssk.math2d.normalize local getNormals = ssk.math2d.normals local vecLen = ssk.math2d.length local vecLen2 = ssk.math2d.length2 -- Lua and Corona local mAbs = math.abs local mRand = math.random local getInfo = system.getInfo local getTimer = system.getTimer local strMatch = string.match local strFormat = string.format -- -- A place to store our 'balls' local balls = {} local colors = { \_R\_, \_G\_, \_Y\_, \_C\_ } local colorNames = { "RED", "GREEN", "YELLOW", "CYAN"} local spawnTimer local onCollision local spawnArrow -- -- Target local spinner = display.newImage("circle.png") spinner.x = display.contentWidth/2 spinner.y = display.contentHeight/2 spinner:scale( 0.8, 0.8) local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - spinner.x local dy = e.y - spinner.y adjustment = math.atan2(dy,dx) \* 180 / PI - spinner.rotation end if(e.phase == "moved") then local dx = e.x - spinner.x local dy = e.y - spinner.y spinner.rotation = (math.atan2(dy,dx) \* 180 / PI) - adjustment end end Runtime:addEventListener('touch', onTouch) -- The balls -- -- Min distance to start away is worst case (i.e. Corner of screen) local vec = subVec( centerX, centerY, left, top, true) local minLen = vecLen( vec ) onCollision = function( self, event ) if( event.phase == "began" ) then balls[self] = nil print("My approach angle: ", self.approachAngle, " color: ", colorNames[self.myColor]) display.remove( self) end return true end spawnArrow = function() local size = 20 local speed = 200 local approachAngle = math.random(0,360) local startOffset = angle2Vector( approachAngle, true ) local velocityVec = table.shallowCopy( startOffset ) startOffset = scaleVec( startOffset, minLen ) velocityVec = scaleVec( velocityVec, -speed ) local tmp = display.newCircle( startOffset.x + centerX, startOffset.y + centerY, size/2 ) physics.addBody( tmp, "dynamic", { radius = size/2 } ) tmp.isSensor = true tmp.approachAngle = approachAngle tmp.myColor = math.random(1,#colors) tmp:setFillColor( unpack(colors[tmp.myColor]) ) tmp.collision = onCollision tmp:addEventListener( "collision" ) local function hitTestObjects(tmp, spinner) local left = tmp.contentBounds.xMin \<= spinner.contentBounds.xMin and tmp.contentBounds.xMax \>= spinner.contentBounds.xMin local right = tmp.contentBounds.xMin \>= spinner.contentBounds.xMin and tmp.contentBounds.xMin \<= spinner.contentBounds.xMax local up = tmp.contentBounds.yMin \<= spinner.contentBounds.yMin and tmp.contentBounds.yMax \>= spinner.contentBounds.yMin local down = tmp.contentBounds.yMin \>= spinner.contentBounds.yMin and tmp.contentBounds.yMin \<= spinner.contentBounds.yMax return (left or right) and (up or down) end local doTest = true local function test(event) --Check if the objects are colliding and are the same color if doTest == true then if hitTestObjects(tmp, spinner) == true then if tmp.color == spinner.color then print("collided with same color object") --Remove the images display.remove(tmp) doTest = false end else spinner:translate(0, 0) end end return true end Runtime:addEventListener("enterFrame", test) tmp:setLinearVelocity( velocityVec.x, velocityVec.y ) balls[tmp] = tmp timer.performWithDelay( 3000, spawnArrow ) end spawnArrow()
I have that collision ( balls with circle ) but in corona simulator output it just says collided with same color object and on Corona simulator balls are coming across the screen with random colors from code but I want to make that when e.g. yellow ball hits red part on circle it just opens restart screen.
Sorry if you don’t understand,I am from Croatia 