[Resolved] Items Bar Again

Hey, it’s me with the items bar again! This time, what I want to do is completely different. Here it is:

By the way: If you have seen my ‘items bar lock view’ forum topic, you’ll know what my items bar is. If you haven’t visited that topic, do so now.

  1. What I want to do:
    I want to use my external library dragIt (created from movieclip.lua) to make a drag function that only drags if the touch is not in the items bar.

  2. My dragIt code: (modified for items bar dilemma)

module(..., package.seeall)  
  
 local function dragMe(self, event)  
 local g=event.target  
 local onPress = self.\_onPress  
 local onDrag = self.\_onDrag  
 local onRelease = self.\_onRelease  
 local iCrossedOver=false  
 function decideIfCrossed()  
 if event.y\<119 then  
 iCrossedOver=true  
 end  
 end  
 decideIfCrossed()  
 if event.phase == "began" and (not iCrossedOver) then  
 decideIfCrossed()  
 display.getCurrentStage():setFocus( self )   
  
 g.\_startX = event.x - g.x  
 g.\_startY = event.y - g.y  
  
 if onPress then  
 result = onPress( event )  
 end  
  
 elseif event.phase == "moved" and (not iCrossedOver) then  
 decideIfCrossed()  
 if transpose == true then  
 if limitX ~= true then  
 g.x = g.\_startX - (event.yStart - event.y)  
 end  
 if limitY ~= true then  
 g.y = g.\_startY + (event.xStart - event.x)  
 end  
 else  
 if limitX ~= true then  
 g.x = event.x - g.\_startX  
 if (dragBounds) then  
 if (g.x \< dragLeft) then g.x = dragLeft end  
 if (g.x \> dragLeft + dragWidth) then g.x = dragLeft + dragWidth end  
 end  
 end  
 if limitY ~= true then  
 g.y = event.y - g.\_startY  
 if (dragBounds) then  
 if (g.y \< dragTop) then g.y = dragTop end  
 if (g.y \> dragTop + dragHeight) then g.y = dragTop + dragHeight end  
 end  
 end   
  
 end  
  
 if onDrag then  
 result = onDrag( event )  
 end  
  
 elseif event.phase == "ended" and (not iCrossedOver) then  
 decideIfCrossed()  
 display.getCurrentStage():setFocus( nil )  
 gX=g.x  
 gY=g.y  
 if onRelease then  
 self.y=gY  
 self.x=gX  
 result = onRelease( event )  
 end  
  
 end  
  
 return true  
 end  
  
 function setDrag( item, params )  
 if ( params ) then  
 if params.drag == true then  
 limitX = (params.limitX == true)  
 limitY = (params.limitY == true)  
 transpose = (params.transpose == true)  
 dragBounds = nil  
  
 if ( params.onPress and ( type(params.onPress) == "function" ) ) then  
 item.\_onPress = params.onPress  
 end  
 if ( params.onDrag and ( type(params.onDrag) == "function" ) ) then  
 item.\_onDrag = params.onDrag  
 end  
 if ( params.onRelease and ( type(params.onRelease) == "function" ) ) then  
 item.\_onRelease = params.onRelease  
 end  
 if ( params.bounds and ( type(params.bounds) == "table" ) ) then  
 dragBounds = params.bounds  
 dragLeft = dragBounds[1]  
 dragTop = dragBounds[2]  
 dragWidth = dragBounds[3]  
 dragHeight = dragBounds[4]  
 end  
  
 item.touch = dragMe  
 item:addEventListener( "touch", item )  
  
 else  
 dragBounds = nil  
  
 end  
 end  
 end  
  1. What my problem is:
    I tried setting it to only move if the touch is under 119 (my items bar size), but if you put your finger (or pointer) over 119 after starting it in 119 or moving into 119, it makes a horribly jerky movement. I guess because it’s trying to line the main screen up with your touch.

  2. What is happening:
    I can identify the problem. It is checking if iCrossedOver every time you move the touch. So event if you started or moved into 119, it is resetting it once you move out. I need a way to if you began or moved into 119, it to instantly cancel the movement. How would I do that?

binc

[import]uid: 147322 topic_id: 27580 reply_id: 327580[/import]

Hello? Better yet, if there was a way to set the touch point to where you left the bar instead of your original point when you crossed into 119, that would be much appreciated. [import]uid: 147322 topic_id: 27580 reply_id: 112075[/import]

You may want to link to your other topic if you are requesting people view it :wink:

Do you have anything plug and play or downloadable? [import]uid: 52491 topic_id: 27580 reply_id: 112143[/import]

Sorry, this is going to take a while. I’m getting together an example. I’ll just bump it when I’m finished. [import]uid: 147322 topic_id: 27580 reply_id: 112230[/import]

Ok, here’s a few things:
Items bar lock view

Also,

M={}  
  
function new()  
  
game=display.newGroup()  
  
director=require("director")  
movieclip=require ("movieclip")  
ui = require( "ui" )  
physics = require "physics"  
physics.start()  
physics.setGravity( 0,12 )  
require( "ice" )  
dragIt=require("dragIt") -- external lib  
  
iPad=false  
iPhone=false  
function setModel() -- going to make two versions, for iPhone and iPad, so graphics go better with screen  
 model=system.getInfo( "model" )  
 if "iPad"==model then  
 iPad=true  
 elseif "iPhone"==model or "iPod touch"==model then  
 iPhone=true  
 end  
end  
setModel()  
  
function iPadFormat() -- for iPad  
  
veryback=display.newGroup()  
back=display.newGroup()  
front=display.newGroup()  
  
scrollGroup=display.newGroup() --set up scrolling and non scrolling groups, for tiles  
scrollGroup:insert(veryback)  
scrollGroup:insert(back)  
scrollGroup:insert(front)  
game:insert(scrollGroup)  
  
nonScroller=display.newGroup()  
game:insert(nonScroller)  
  
local height=display.contentHeight  
local width=display.contentWidth  
local middleY = display.contentHeight/2  
local middleX = display.contentWidth/2  
  
local ground=display.newRect(0,0,2048,10)  
physics.addBody ( ground, "static", {bounce=0.1, friction=35})  
ground.x, ground.y=1024, 695  
veryback:insert(ground)  
local p1=display.newRect(0,0,220,44)  
p1.x, p1.y=573,645  
p1.rotation=-45  
physics.addBody ( p1, "static", {friction=35, bounce=0.1})  
veryback:insert(p1)  
local p2=display.newRect(0,0,25,150)  
p2.x, p2.y=1330, 650  
physics.addBody ( p2, "static", {friction=35, bounce=0.1 } )  
veryback:insert(p2)  
  
local rect=display.newRect(0, 0, 2048, 768)  
rect.alpha=0  
rect.isHitTestable=true  
veryback:insert(rect)  
--NOTE: I have to make this rect to return something for dragIt to drag, as if you remove this rect, the screen is dragged only if you touch something in the drag group.  
---------------------------------------------------  
  
local t1=display.newImage("tile.png") -- just create your own image for this and t2, just display objects to illustrate items bar  
t1.x, t1.y=51.5,51.5  
  
local t2=display.newImage("tile2.png")  
t2.x, t2.y=151.5,51.5  
  
local ball=display.newImage("ball-red.png") -- use an ball with a radius of 45, or change the physics radius below.  
ball.x, ball.y=80,480  
ball.name="ball"  
front:insert(ball)  
physics.addBody(ball, {bounce=0.2, radius=45, friction=100})  
  
local function moveCamera()  
 if (ball.x \> 512 and ball.x \< 1536) then  
 scrollGroup.x = -ball.x + 512  
 end  
end  
Runtime:addEventListener( "enterFrame", moveCamera )  
dragIt.setDrag( scrollGroup, { -- set my drag for group  
 drag=true,  
 bounds={-1024, 0, 1024, 0}  
})  
  
end  
iPadFormat()  
  
return game --return a group for director  
  
end  
  
M.new=new  
return M  

for dragIt source code:
dragIt source code

If I could know how to make a new drag function in dragIt, explicitly for this problem, with the idea of making it reset your touch point when you leave the items bar, that would be great. I would not like to cancel the movement anymore. [import]uid: 147322 topic_id: 27580 reply_id: 112433[/import]

Resolved! All I had to do was, if it crossed over, reset the g._startX and g._startY.

binc [import]uid: 147322 topic_id: 27580 reply_id: 112540[/import]

Wow, OK, that was fast! Well done and thanks for updating :slight_smile: [import]uid: 52491 topic_id: 27580 reply_id: 112621[/import]

Sorry for all of the trouble :slight_smile:

binc [import]uid: 147322 topic_id: 27580 reply_id: 112651[/import]