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:
[lua]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)[/lua]
Any suggestion would be awesome!
Thanks
[import]uid: 51459 topic_id: 16642 reply_id: 316642[/import]