I have 20 apples on a tree that are attached to a table.. One apple that is attached to a onEnterFrame(event) Which makes it fall and draggable.
I have a tree that animates a shake..
Here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | local loqsprite = require('loq_sprite') display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR local screenW = display.contentWidth local screenH = display.contentHeight local goalsAchieved = 0 local rand = math.random local i local appleCount = 0 local apples = {} local apple local counter = 0 local applesOnScreen = 0 local maxApples = 20 local _apple = {} local speed = 4 local appleIcons = {} local t1 = { { x=490, y=380 }, { x=400, y=380 }, { x=550, y=350 }, { x=450, y=250 }, { x=540, y=130 }, { x=530, y=290 }, { x=500, y=200 }, { x=550, y=230 }, { x=600, y=280 }, { x=500, y=250 }, { x=500, y=200 }, { x=650, y=390 }, { x=610, y=420 }, { x=565, y=425 }, { x=600, y=200 }, { x=425, y=290 }, { x=470, y=300 }, { x=540, y=180 }, { x=610, y=350 }, { x=455, y=342 }, } local BG = display.newImageRect("background.png", 1024, 768) BG.x = screenW/2; BG.y = screenH/2 local tfactory = loqsprite.newFactory("aniTree") local theTree = tfactory:newSpriteGroup() theTree.x = screenW/2; theTree.y = 300 local basket = display.newImageRect("basket.png", 224, 165) basket.x = 850; basket.y = 625 local apple = display.newImageRect("apple.png", 39, 44) apple.x = screenW/4; apple.y = screenH/4 local goalCount = display.newText(string.format("%02d", 0), 0, 0, native.systemFontBold, 40) goalCount:setTextColor(255,255,255,255) goalCount.x = 850; goalCount.y = 600 function spawnApple() local theApple = _apple.new() if theApple == nil then return end apples[theApple.ID] = theApple applesOnScreen = 1 print("appless on screen", applesOnScreen) end local function isGoalReached(event) goalReached = false if event.phase == "ended" then goalReached = true; end end function onEnterFrame(event) if apple.isFocus==true then return end if apple.y < 500 then apple.y = apple.y+speed end end Runtime:addEventListener("enterFrame",onEnterFrame) for i = 1,#t1 do apples[i] = display.newImageRect("apple.png", 39, 44) apples[i].x = t1[i].x; apples[i].y = t1[i].y end local function shakeTree(event) theTree:play("treeShake shake") end theTree:addEventListener("tap", shakeTree) --[[ local function drawAppleImg(num) for i = 1, 10 do if i <= num then apples[i].isVisible = true else apples[i].isVisible = false end end end ]] local function dragApple(event) local phase = event.phase local x = event.x local y = event.y local target = event.target if "began" == phase then display.currentStage:setFocus(apple) target.x0 = x target.y0 = y target.isFocus = true elseif "moved" == phase and target.isFocus==true then target.x = target.x + (x-target.x0) target.y = target.y + (y-target.y0) target.x0 = x target.y0 = y elseif "ended" == phase then display.currentStage:setFocus(nil) apple.isFocus = false target.x0 = nil target.y0 = nil end end apple:addEventListener("touch", dragApple) basket:addEventListener("touch", isGoalReached) |
you know Jake, this might be a case of where you want to mix physics with your non-physics code. Let the physics manage the falling apples. Its the "Newtonian" thing to do.
Instead of writing your own drop code, (which is a good exercise by the way...) this is where physics is a perfect use. Just make your apples physics bodies, put in a floor for the apples to fall too and let them bounce around, fall at the right speeds etc.
I've only used physics a little, so I'm not going to be a good source to help you.
I don't necessarily have to make them fall, just remove a random amount of them when the tree is clicked.
Well to select a random apple and just remove it, I'd do this:
1 2 3 4 5 | appleToRemove = random(#apples) apples[appleToRemove]:removeSelf() apples[appleToRemove] = nil table.remove(apples, appleToRemove) |
Thanks Rob!! This is a great chunk of code to work with!