Hi, I used example from: https://github.com/dmccuskey/DMC-Corona-Library/blob/master/examples/dmc_dragdrop and trying to adjust it to my needs
Most important is the class drop_target.lua:
--====================================================================-- -- OO Drop Target -- -- Sample code is MIT licensed, the same license which covers Lua itself -- http://en.wikipedia.org/wiki/MIT\_License -- Copyright (C) 2011-2015 David McCuskey. All Rights Reserved. --====================================================================-- --===================================================================-- --== Imports local DragMgr = require 'dmc\_corona.dmc\_dragdrop' local Objects = require 'dmc\_corona.dmc\_objects' local Utils = require 'dmc\_corona.dmc\_utils' --===================================================================-- --== Setup, Constants -- setup some aliases to make code cleaner local newClass = Objects.newClass local ComponentBase = Objects.ComponentBase --===================================================================-- --== Support Functions -- createSquare() -- -- function to help create shapes, useful for drag/drop target examples -- local function createSquare( params ) params = params or {} assert( type(params)=='table', "createSquare requires params" ) assert( params.height and params.width, "createSquare requires height and width" ) if params.fillColor==nil then params.fillColor=DragMgr.COLOR\_LIGHTGREY end if params.strokeColor==nil then params.strokeColor=DragMgr.COLOR\_GREY end if params.strokeWidth==nil then params.strokeWidth=3 end --==-- local o = display.newRect(0, 0, params.width, params.height ) o.strokeWidth = params.strokeWidth o:setFillColor( unpack( params.fillColor ) ) o:setStrokeColor( unpack( params.strokeColor ) ) return o end --====================================================================-- --== Drop Target Class --====================================================================-- local DropTargetIcon = newClass( ComponentBase, {name="Drop Target Icon"} ) --======================================================-- -- Start: Setup DMC Objects -- \_\_init\_\_() -- called by new() -- one of the base methods to override for dmc\_objects -- here we save params, initialize basic properties, etc -- function DropTargetIcon:\_\_init\_\_( params ) -- print( "DropTargetIcon:\_\_init\_\_" ) self:superCall( '\_\_init\_\_', params ) params = params or {} if params.format == nil then params.format = {} end if params.color == nil then params.color = DragMgr.COLOR\_LIGHTGREY end if params.icon\_id == nil then params.icon\_id = 0 end if params.name == nil then params.name = 'Unknown' end --==-- if type(params.format) =='string' then params.format = { params.format } end --== Create Properties ==-- self.icon\_id = params.icon\_id self.name = params.name self.img = params.img self.\_score = 0 self.\_format = params.format self.\_color = params.color -- { 255, 25, 255 } self.\_background = nil self.\_scoreboard = nil end -- \_\_createView\_\_() -- -- one of the base methods to override for dmc\_objects -- here we put on our display properties -- function DropTargetIcon:\_\_createView\_\_() -- print( "DropTargetIcon:\_\_createView\_\_" ) self:superCall( '\_\_createView\_\_' ) --==-- local o -- background o = createSquare{ width=60, height=60, fillColor=self.\_color } o.anchorX, o.anchorY = 0.5, 0.5 o.x, o.y = 0, 0 self:insert( o ) self.\_background = o -- self.icon\_id = params.icon\_id -- self.name = params.name -- self.img = params.img o = display.newImageRect(self.img, 50, 50) o.icon\_id = self.icon\_id o.name = self.name o.anchorX, o.anchorY = 0.5, 0.5 o.x, o.y = 0, 0 -- -- print("name: ",obj.name) -- obj.y = y -- obj.x = x self:insert( o ) -- self.\_background = o -- scoreboard o = display.newText( self.name, 0, 0, native.systemFont, 14 ) o:setTextColor( 0, 0, 0, 255 ) o.anchorX, o.anchorY = 0.5, 0.5 o.x, o.y = 0, 40 self:insert( o ) self.\_scoreboard = o end -- \_\_initComplete\_\_() -- -- post init actions -- base dmc\_object override -- function DropTargetIcon:\_\_initComplete\_\_() -- print( "DropTargetIcon:\_\_initComplete\_\_" ) self:superCall( '\_\_initComplete\_\_' ) --==-- -- draw initial score self:\_updateScore() end -- END: Setup DMC Objects --======================================================-- --====================================================================-- --== Public Methods -- none --====================================================================-- --== Private Methods function DropTargetIcon:\_incrementScore() -- self.\_score = self.\_score + 1 -- self:\_updateScore() end function DropTargetIcon:\_updateScore() -- self.\_scoreboard.text = tostring( self.\_score ) -- self.\_scoreboard.x, self.\_scoreboard.y = 0, 0 end --====================================================================-- --== Event Handlers --== define method handlers for each drag phase function DropTargetIcon:dragStart( e ) local data\_format = e.format -- loop over the data formats and see if we match if true or Utils.propertyIn( self.\_format, data\_format ) then self.\_background:setStrokeColor( unpack( DragMgr.COLOR\_RED ) ) end return true end function DropTargetIcon:dragEnter( e ) -- must accept drag here local data\_format = e.format -- loop over the data formats and see if we match if Utils.propertyIn( self.\_format, data\_format ) then self.\_background:setFillColor( unpack( DragMgr.COLOR\_LIGHTGREEN ) ) DragMgr:acceptDragDrop() end return true end function DropTargetIcon:dragOver( e ) return true end function DropTargetIcon:dragDrop( e ) self:\_incrementScore() self:dragExit( e ) return true end function DropTargetIcon:dragExit( e ) self.\_background:setFillColor( unpack( self.\_color ) ) return true end function DropTargetIcon:dragStop( e ) self.\_background:setStrokeColor( unpack( DragMgr.COLOR\_GREY ) ) return true end return DropTargetIcon
I’m using it like:
dropTargetIcon = DropTargetIcon:new{ icon\_id=i, img="./someicon", name="example" } DragMgr:register( dropTargetIcon ) dropTargetIcon:addEventListener( 'touch', dragItemLightredTouchHandler )
all works fine, but I have problems whenever I would like to put that object into widget scrollview, or call display.currentStage:setFocus(dropTargetIcon… or display.currentStage:insert( dropTargetIcon )
I get that metatable is not display object, but I’m wondering how can I keep having DropTargetIcon functionality and have option to use is as a displayobject.
Can you give me any hints?