Grappling hook

Hello there, I’m trying to implement “gold miner” grappeling hook into my game. the hook in my game is spinning 360 degrees in the center of the screen. I am stuck in writing the tap event for it… on tap the hook will stop spinning and go foward in the same direction until it hits an object or wall then go back to i’ts start pos exactly like “gold miner”.

Can someone help me with that piece of code?

Thanks,

Itay

  1. You will want to add a tap/touch listener to the hook sprite. (A quick search of the docs has many examples)

  2. Not knowing how you ‘spin’ it, you will need to know the angle you’re pointing at. Tricky to direct you without knowing how you are spinning it right now. (ssk2 has some useful code/samples I believe)

  3. With that angle you can then add a collision listener to what you fire, or a check for edge of screen (or a sensor off screen).  (Again, the docs have some good collision examples)

May be enough for you to narrow your search and find some answers.

https://www.youtube.com/watch?v=nODzbmZXuJw&feature=youtu.be

Forgive the formatting this is about 20 minutes of coding.  I didn’t spend any time on polish.

Code (works with SSK2 lite or PRO): https://github.com/roaminggamer/RG_FreeStuff/raw/master/SSK2/forums_help/goldMiner.zip

SSK2:

  • PRO - https://marketplace.coronalabs.com/asset/super-starter-kit-2-ssk2-pro

  • lite - https://marketplace.coronalabs.com/asset/super-starter-kit-2-ssk2-lite

    – ============================================================= – Copyright Roaming Gamer, LLC. 2008-2017 (All Rights Reserved) – ============================================================= – main.lua – ============================================================= – Start (code/comments) – ============================================================= io.output():setvbuf(“no”) display.setStatusBar(display.HiddenStatusBar) – ============================================================= – LOAD & INITIALIZE - SSK 2 – ============================================================= require “ssk2.loadSSK” _G.ssk.init() _G.ssk.init( {} ) – -- Super Basic Gold Miner – local hook local background local minRot = 110 local maxRot = 250 local rotRate = -60 – degrees per second local outSpeed = 300 – pixels per second local inSpeed = 300 – pixels per second local snapDist = 10 local lastT = system.getTimer() local doNothing local waitingToFire local hookMovingOut local retrievingHook local objects = {} doNothing = function( self ) lastT = system.getTimer() end waitingToFire = function( self, event ) local curT = system.getTimer() local dt = curT - lastT lastT = curT self.rotation = self.rotation + rotRate * dt/1000 if( self.rotation < minRot ) then rotRate = -rotRate elseif( self.rotation > maxRot ) then rotRate = -rotRate end end hookMovingOut = function( self ) local curT = system.getTimer() local dt = curT - lastT lastT = curT local vec = ssk.math2d.angle2Vector( self.rotation, true ) vec = ssk.math2d.scale(vec, outSpeed * dt/1000 ) self.x = self.x + vec.x self.y = self.y + vec.y display.remove( self.line ) self.line = display.newLine( self.x, self.y, self.x0, self.y0) self:toFront() – Hit Something? for k, v in pairs( objects ) do if( ssk.easyIFC.isInBounds( self, v ) ) then self.dragObj = v self.enterFrame = retrievingHook return end end – Out Of Bounds if( not ssk.easyIFC.isInBounds( self, background ) ) then self.enterFrame = retrievingHook end end retrievingHook = function( self ) local curT = system.getTimer() local dt = curT - lastT lastT = curT local vec = ssk.math2d.angle2Vector( self.rotation, true ) vec = ssk.math2d.scale(vec, inSpeed * dt/1000 ) self.x = self.x - vec.x self.y = self.y - vec.y display.remove( self.line ) self.line = display.newLine( self.x, self.y, self.x0, self.y0) – Dragging something? if( self.dragObj ) then self.dragObj.x = self.dragObj.x - vec.x self.dragObj.y = self.dragObj.y - vec.y end – Back Home? local vec = ssk.math2d.sub( self.x, self.y, self.x0, self.y0, true ) local len = ssk.math2d.length( vec ) if( len < snapDist ) then self.x = self.x0 self.y = self.y0 if( self.dragObj ) then objects[self.dragObj] = nil display.remove( self.dragObj ) self.dragObj = nil end display.remove( self.line ) self.enterFrame = waitingToFire end self:toFront() end local function onTouch( self, event ) if( event.phase ~= “ended” ) then return true end if( hook.enterFrame == waitingToFire ) then hook.enterFrame = hookMovingOut end end background = ssk.display.newRect( nil, centerX, centerY, { w = fullw, h = fullh, fill = hexcolor(’#76472b’), touch = onTouch }) local miner = ssk.display.newImageRect( nil, centerX, top + 80, “images/kenney3.png”, { size = 100 } ) hook = ssk.display.newImageRect( nil, miner.x, miner.y + 50, “images/arrow.png”, { rotation = 180, size = 30, enterFrame = doNothing } ) hook.x0 = hook.x hook.y0 = hook.y hook.enterFrame = waitingToFire for i = 1, 9 do local obj = ssk.display.newImageRect( nil, 75 + (i-1) * 100, bottom - 100, “images/coin.png”, { size = math.random( 50, 80 ) } ) objects[obj] = obj end

Nicely done.

  1. You will want to add a tap/touch listener to the hook sprite. (A quick search of the docs has many examples)

  2. Not knowing how you ‘spin’ it, you will need to know the angle you’re pointing at. Tricky to direct you without knowing how you are spinning it right now. (ssk2 has some useful code/samples I believe)

  3. With that angle you can then add a collision listener to what you fire, or a check for edge of screen (or a sensor off screen).  (Again, the docs have some good collision examples)

May be enough for you to narrow your search and find some answers.

https://www.youtube.com/watch?v=nODzbmZXuJw&feature=youtu.be

Forgive the formatting this is about 20 minutes of coding.  I didn’t spend any time on polish.

Code (works with SSK2 lite or PRO): https://github.com/roaminggamer/RG_FreeStuff/raw/master/SSK2/forums_help/goldMiner.zip

SSK2:

  • PRO - https://marketplace.coronalabs.com/asset/super-starter-kit-2-ssk2-pro

  • lite - https://marketplace.coronalabs.com/asset/super-starter-kit-2-ssk2-lite

    – ============================================================= – Copyright Roaming Gamer, LLC. 2008-2017 (All Rights Reserved) – ============================================================= – main.lua – ============================================================= – Start (code/comments) – ============================================================= io.output():setvbuf(“no”) display.setStatusBar(display.HiddenStatusBar) – ============================================================= – LOAD & INITIALIZE - SSK 2 – ============================================================= require “ssk2.loadSSK” _G.ssk.init() _G.ssk.init( {} ) – -- Super Basic Gold Miner – local hook local background local minRot = 110 local maxRot = 250 local rotRate = -60 – degrees per second local outSpeed = 300 – pixels per second local inSpeed = 300 – pixels per second local snapDist = 10 local lastT = system.getTimer() local doNothing local waitingToFire local hookMovingOut local retrievingHook local objects = {} doNothing = function( self ) lastT = system.getTimer() end waitingToFire = function( self, event ) local curT = system.getTimer() local dt = curT - lastT lastT = curT self.rotation = self.rotation + rotRate * dt/1000 if( self.rotation < minRot ) then rotRate = -rotRate elseif( self.rotation > maxRot ) then rotRate = -rotRate end end hookMovingOut = function( self ) local curT = system.getTimer() local dt = curT - lastT lastT = curT local vec = ssk.math2d.angle2Vector( self.rotation, true ) vec = ssk.math2d.scale(vec, outSpeed * dt/1000 ) self.x = self.x + vec.x self.y = self.y + vec.y display.remove( self.line ) self.line = display.newLine( self.x, self.y, self.x0, self.y0) self:toFront() – Hit Something? for k, v in pairs( objects ) do if( ssk.easyIFC.isInBounds( self, v ) ) then self.dragObj = v self.enterFrame = retrievingHook return end end – Out Of Bounds if( not ssk.easyIFC.isInBounds( self, background ) ) then self.enterFrame = retrievingHook end end retrievingHook = function( self ) local curT = system.getTimer() local dt = curT - lastT lastT = curT local vec = ssk.math2d.angle2Vector( self.rotation, true ) vec = ssk.math2d.scale(vec, inSpeed * dt/1000 ) self.x = self.x - vec.x self.y = self.y - vec.y display.remove( self.line ) self.line = display.newLine( self.x, self.y, self.x0, self.y0) – Dragging something? if( self.dragObj ) then self.dragObj.x = self.dragObj.x - vec.x self.dragObj.y = self.dragObj.y - vec.y end – Back Home? local vec = ssk.math2d.sub( self.x, self.y, self.x0, self.y0, true ) local len = ssk.math2d.length( vec ) if( len < snapDist ) then self.x = self.x0 self.y = self.y0 if( self.dragObj ) then objects[self.dragObj] = nil display.remove( self.dragObj ) self.dragObj = nil end display.remove( self.line ) self.enterFrame = waitingToFire end self:toFront() end local function onTouch( self, event ) if( event.phase ~= “ended” ) then return true end if( hook.enterFrame == waitingToFire ) then hook.enterFrame = hookMovingOut end end background = ssk.display.newRect( nil, centerX, centerY, { w = fullw, h = fullh, fill = hexcolor(’#76472b’), touch = onTouch }) local miner = ssk.display.newImageRect( nil, centerX, top + 80, “images/kenney3.png”, { size = 100 } ) hook = ssk.display.newImageRect( nil, miner.x, miner.y + 50, “images/arrow.png”, { rotation = 180, size = 30, enterFrame = doNothing } ) hook.x0 = hook.x hook.y0 = hook.y hook.enterFrame = waitingToFire for i = 1, 9 do local obj = ssk.display.newImageRect( nil, 75 + (i-1) * 100, bottom - 100, “images/coin.png”, { size = math.random( 50, 80 ) } ) objects[obj] = obj end

Nicely done.