As of now my code shows 20 apples on a tree.. When you click on the tree the tree shakes.. Nothing happens to the apples
I have a code that generates random apples on the ground. Which is connected to the spawnApples function. I set it up so that it up so that nothing is showing on the ground.
What I am uncertain of doing is removing a random number of apples on the tree and appear on the ground once the tree is clicked.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 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 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 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 hitTestObjects(obj1, obj2) local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax return (left or right) and (up or down) end function spawnApple() --if applesOnScreen >= maxApples then return end 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 applesOnScreen < 0 then spawnApple() 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 onTouch(event) local phase = event.phase local x = event.x local y = event.y local target = event.target if "began" == phase then display.currentStage:setFocus(theApple) 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) theApple.isFocus = false target.x0 = nil target.y0 = nil end end function _apple.new() local aApple = display.newImageRect("apple.png", 39, 44) minX = 250 maxX = 650 minY = 420 + aApple.contentHeight maxY = display.contentHeight - aApple.contentHeight aApple.x = rand(minX, maxX) aApple.y = rand(minY, maxY) --aApple.x = rand(780 - aApple.contentWidth) --aApple.y = rand(900 / 2, screenH - aApple.contentHeight) aApple.rotation = rand(360) aApple.ID = "Item_" .. counter return aApple end --theApple:addEventListener("touch", dragApple) basket:addEventListener("touch", isGoalReached) |
Hey.
I'm not sure I understand your question correctly.
You say you've programmed an app which displays 20 apples on screen already.
At what point do you not have 20 on screen? what interaction does the user make to reduce the amount of apples?
From your code, (around line 90) you have an argument to control the amount of apples on screen, does it work as you intended?
UPDATED
OK so this may make more sense. I want the spawnApples function to generate a random number from 1-20... When added to whats on the tree the # of apples should always be 20 on the screen.
How about...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | local function spawnApple(thisOne) local apple = display.newImage("apple.png") apple.id = i local x = math.random(1,_W) local y = math.random(1,_H) apple:setReferencePoint(display.TopLeftReferencePoint) apple.x = x apple.y = y local function dragApple(event) ----write your handler here end apple:addEventListener("touch",dragApple) return apple end |
Wow! You just blow my mind JayantV!
You taught me well!
THANKS!
Jake
Wait I spoke too soon.. :( I'm getting an error which is this
Syntax error: ...folders/NS/NS3zjtx7HZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/454/main.lua:136: '=' expected near 'end'
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 153 154 155 156 157 158 159 160 161 162 | 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 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 hitTestObjects(obj1, obj2) local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax return (left or right) and (up or down) end local function spawnApple(thisOne) local apple = display.newImage("apple.png") apple.id = i local x = math.random(1,_W) local y = math.random(1,_H) apple:setReferencePoint(display.TopLeftReferencePoint) apple.x = x apple.y = y 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) return apple end local function isGoalReached(event) goalReached = false if event.phase == "ended" then goalReached = true; end end 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() local numberOfApples = firstRandom(1,20) local i for i=1, numberOfApples do spawnApple(i) end local firstRandom = math.random(1,20) local secondSet = 20 - firstRandom for i=1,firstRandom do secondSet end end theTree:addEventListener("tap", shakeTree) function _apple.new() local aApple = display.newImageRect("apple.png", 39, 44) minX = 250 maxX = 650 minY = 420 + aApple.contentHeight maxY = display.contentHeight - aApple.contentHeight aApple.x = rand(minX, maxX) aApple.y = rand(minY, maxY) --aApple.x = rand(780 - aApple.contentWidth) --aApple.y = rand(900 / 2, screenH - aApple.contentHeight) aApple.rotation = rand(360) aApple.ID = "Item_" .. counter return aApple end basket:addEventListener("touch", isGoalReached) |
Do I have to return shakeTree?
because your lines 131 - 135 make no sense
local firstRandom = math.random(1,20)
local secondSet = 20 - firstRandom
for i=1,firstRandom do
secondSet
end
what is secondSet? if it is a variable, then the code here thinks it is a function. What are you trying to do with that??
cheers,
?:)
Ok I changed the bottom a little
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 | local function shakeTree() local numberOfApples = firstRandom(1,20) local i for i=1, numberOfApples do spawnApple(i) end for i=1,firstRandom do _apple.new() end end theTree:addEventListener("tap", shakeTree) function _apple.new() local aApple = display.newImageRect("apple.png", 39, 44) minX = 250 maxX = 650 minY = 420 + aApple.contentHeight maxY = display.contentHeight - aApple.contentHeight aApple.x = rand(minX, maxX) aApple.y = rand(minY, maxY) --aApple.x = rand(780 - aApple.contentWidth) --aApple.y = rand(900 / 2, screenH - aApple.contentHeight) aApple.rotation = rand(360) aApple.ID = "Item_" .. counter return aApple end |
The problem is when you change things from they are supposed to be...
What is this??
local function shakeTree()
local numberOfApples = firstRandom(1,20)
local i
for i=1, numberOfApples do
spawnApple(i)
end
for i=1,firstRandom do
_apple.new()
end
you are calling firstRandom as a function and then you are using it as a variable. definitely there will be an issue if it is not defined.
cheers,
?:)
Im more lost then when I started
I'm getting things back to before I went crazy
Ok this is the error I'm getting now
Runtime error
...ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/454/main.lua:76: bad argument #2 to 'rand' (number expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'rand'
...ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/454/main.lua:76: in function 'spawnApple'
...ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/454/main.lua:148: in function <...ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/454/main.lua:144>
?: in function <?:215>
with this 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 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 hitTestObjects(obj1, obj2) local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax return (left or right) and (up or down) end local function spawnApple(thisOne) local apple = display.newImage("apple.png") apple.id = i local x = rand(1,_W) local y = rand(1,_H) apple:setReferencePoint(display.TopLeftReferencePoint) apple.x = x apple.y = y 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 end apple:addEventListener("touch",dragApple) return apple 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 < 700 then apple.y = apple.y+speed --if applesOnScreen < 0 then --spawnApple() --end --end 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() local numberOfApples = math.random(1,20) local i for i=1, numberOfApples do spawnApple(i) end 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 ]] --[[ function _apple.new() local aApple = display.newImageRect("apple.png", 39, 44) minX = 250 maxX = 650 minY = 420 + aApple.contentHeight maxY = display.contentHeight - aApple.contentHeight aApple.x = rand(minX, maxX) aApple.y = rand(minY, maxY) --aApple.x = rand(780 - aApple.contentWidth) --aApple.y = rand(900 / 2, screenH - aApple.contentHeight) aApple.rotation = rand(360) aApple.ID = "Item_" .. counter return aApple end ]] basket:addEventListener("touch", isGoalReached) |
Knowing you,
a couple of more iterations and you will get it just fine.
keep at it. The answers are with you, keep searching...
cheers,
?:)
It was your words of encouragement!!
I'm getting it!!
I got out of that black hole!
Cheers,
=^D
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed