how to use director class - coronasdk

hey i'm beginner to corona . i'm confused alot about using director class in my first game and it didnt work.here is one problem which i want to be sort out.how can i insert these to local group????its my first app so please help me.
local tree = {}
tree[1] = display.newImage( "Palm-arecaceae.png" )
tree[1].xScale = 0.7; tree[1].yScale = 0.7
tree[1]:setReferencePoint( display.BottomCenterReferencePoint )
tree[1].x = 20; tree[1].y = baseline + 40
tree[1].dx = 0.1
tree[2] = display.newImage( "Greenhouse-Palm-jubaea01.png" )
tree[2].xScale = 0.6; tree[2].yScale = 0.6
tree[2]:setReferencePoint( display.BottomCenterReferencePoint )
tree[2].x = 120; tree[2].y = baseline + 40
tree[2].dx = 0.2

i think that you just have to replace the first line of your code.
local tree = {}
creates a generic table, DisplayObject are special tables, you can create them using:
local tree = display.newGroup()
Hope this helps

You can create your localgroup and insert them one by one with for like following:
local localGroup = display.newGroup()
local tree = {}
tree[1] = display.newImage( "Palm-arecaceae.png" )
tree[1].xScale = 0.7; tree[1].yScale = 0.7
tree[1]:setReferencePoint( display.BottomCenterReferencePoint )
tree[1].x = 20; tree[1].y = baseline + 40
tree[1].dx = 0.1
tree[2] = display.newImage( "Greenhouse-Palm-jubaea01.png" )
tree[2].xScale = 0.6; tree[2].yScale = 0.6
tree[2]:setReferencePoint( display.BottomCenterReferencePoint )
tree[2].x = 120; tree[2].y = baseline + 40
tree[2].dx = 0.2
for i = 1, #tree do
if(tree[i] ~= nil)then
localGroup:insert(transTable[i]))
end

Related

Roblox, how to make something only tween in one direction?

I tried doing
local fakeRightArm = Instance.new("Part")
fakeRightArm.BottomSurface = Enum.SurfaceType.Smooth
fakeRightArm.CanCollide = false
fakeRightArm.Massless = true
fakeRightArm.Material = Enum.Material.Plastic
fakeRightArm.BrickColor = RightArm.BrickColor
fakeRightArm.Size = RightArm.Size
fakeRightArm.CFrame = RightArm.CFrame
fakeRightArm.Parent = character
fakeRightArm.Name = "fakeRightArm"
local weld = Instance.new("WeldConstraint")
weld.Part0 = RightArm
weld.Part1 = fakeRightArm
weld.Parent = character
weld.Name = "FakeArmWeld"
tweenService:Create(fakeRightArm, TweenInfo.new(0.3, Enum.EasingStyle.Quad), {Size = fakeRightArm.Size + Vector3.new(0, 50, 0), CFrame = fakeRightArm.CFrame * CFrame.new(0,0,-50)}):Play()
but that seems to not work. It's probably because of the weld.
I want it so it tweens the arm forward.
DevForum post (maybe more detailed): https://devforum.roblox.com/t/how-to-only-tween-in-one-direction/841408/42
Thank you!
don't think you need the tween function just to position it
https://developer.roblox.com/en-us/api-reference/datatype/CFrame
start with the right arm position, then rotate its coordinate frame.
local fakeRightArm = Instance.new("Part")
fakeRightArm.BottomSurface = Enum.SurfaceType.Smooth
fakeRightArm.CanCollide = false
fakeRightArm.Massless = true
fakeRightArm.Material = Enum.Material.Plastic
fakeRightArm.BrickColor = RightArm.BrickColor
fakeRightArm.Size = RightArm.Size
fakeRightArm.Parent = character
fakeRightArm.Anchored = true
fakeRightArm.Name = "fakeRightArm"
-- you pick which angle to make it face
local Xdir = math.rad( 0 ) -- twist palm up, palm down
local Ydir = math.rad( 90 ) -- lift up / down
local Zdir = math.rad( 15 ) -- swing left / right
fakeRightArm.CFrame = RightArm.Position *CFrame.Angles( Xdir, Ydir, Zdir )

ScrollView doesn't work - corona, lua

I'm trying to add object to the ScrollView and this is what happen:
ERROR:Table expected. if this is a function call, you might have used '.' instead of ':
What is that?
code:
This code is works:
scrollView = widget.newScrollView
{ -- ScrollView settings
width = _W,
height = 1760,
horizontalScrollDisabled = true,
hideBackground = true,
friction = 0.988,
x = display.contentCenterX,
y = _H*0.55,
isBounceEnabled = false,
bottomPadding = 260,
hideScrollBar = true
}
for i = 1, #allballs do
Frames[i] = display.newImage( "Frame.png" )
Frames[i].height = 0.12*_H
Frames[i].width = 0.38*_W
Frames[i].ballImage = allballs[i].Image
Frames[i].Purchased = allballs[i].Purchased
Frames[i].Price = allballs[i].Price
Frames[i].Place = i
if (i%2==0) then
Frames[i].y=(i-1)*140
Frames[i].x = 0.27*_W
PositionY = (i-1)*140
PositionX = 0.27*_W
else
Frames[i].y=i*140
Frames[i].x = 0.73*_W
PositionY = i*140
PositionX = 0.73*_W
end
if (allballs[i].Purchased) then
balls[i] = display.newImage( allballs[i].Image )
balls[i].height = 0.15*_W
balls[i].width = 0.15*_W
balls[i].x = PositionX
balls[i].y = PositionY
else
local txtoptions =
{
text = allballs[i].Price,
x = PositionX+0.055*_W,
y = PositionY,
width = 0.23*_W,
font = native.systemFontBold,
fontSize = _W*0.1,
align = "left"
}
balls[i] = display.newText( txtoptions )
end
Frames[i]:setFillColor( 1, 1, 0.4 )
Frames[i]:addEventListener( "tap", sellectball )
scrollView:insert( Frames[i] )
scrollView:insert( balls[i] )
end
Everything works perfectly. But when I add this (between the ***):
for i = 1, #allballs do
Frames[i] = display.newImage( "Frame.png" )
Frames[i].height = 0.12*_H
Frames[i].width = 0.38*_W
Frames[i].ballImage = allballs[i].Image
Frames[i].Purchased = allballs[i].Purchased
Frames[i].Price = allballs[i].Price
Frames[i].Place = i
if (i%2==0) then
Frames[i].y=(i-1)*140
Frames[i].x = 0.27*_W
PositionY = (i-1)*140
PositionX = 0.27*_W
else
Frames[i].y=i*140
Frames[i].x = 0.73*_W
PositionY = i*140
PositionX = 0.73*_W
end
if (allballs[i].Purchased) then
balls[i] = display.newImage( allballs[i].Image )
balls[i].height = 0.15*_W
balls[i].width = 0.15*_W
balls[i].x = PositionX
balls[i].y = PositionY
else
local txtoptions =
{
text = allballs[i].Price,
x = PositionX+0.055*_W,
y = PositionY,
width = 0.23*_W,
font = native.systemFontBold,
fontSize = _W*0.1,
align = "left"
}
balls[i] = display.newText( txtoptions )
***** coin = display.newImage( "coin.png", PositionX-0.07*_W, PositionY)
coin.width = _W*0.05
coin.height = _H*0.05 *******
end
Frames[i]:setFillColor( 1, 1, 0.4 )
Frames[i]:addEventListener( "tap", sellectball )
scrollView:insert( Frames[i] )
scrollView:insert( balls[i] )
******* scrollView:insert( coin ) ********
end
It dosent work...
youre probably calling the method like object.method() instead of object:method()
difference is that : syntax uses automatically this as reference to caller object, with . syntax you have to type it explicitly as first parameter
The error always occurs for the reason like Andoloon pointed out.
but it doesn't seem to be in your ******* code. The problem for the ***** section might be that your 'coin' var is not a list, but it is defined and inserted in the loop. Try move it out of the loop and also remember to use local before the declearation.
I'm guessing that coin.png doesn't exist, or may not be an exact file name match (if you're doing this on a device). Look for a warning in your console log about missing images.

Add score using score module

I'm working on a game, collecting points by pressing on images. Pressing images and respawning new ones is no problem but adding points to the score causes some problems. When I press on the images the score doesn't get updated with the amount of points. I'm using this score module http://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/
local score_mod = require( "score" )
math.randomseed( os.time() )
local scoreText = score_mod.init({
fontSize = 70,
font = native.systemFont,
x = display.contentCenterX + 50,
y = display.contentCenterY + 170,
filename = "scorefile.txt"
})
scoreText:setTextColor( (232/255), (216/255), (32/255) )
local function Game ()
local images ={
{name = "Icon.png", points = 1},
{name = "Icon-mdpi.png", points = 2},
{name = "Icon-xhdpi.png", points = 3},
{name = "Icon-ldpi.png", points = 4},
{name = "Icon-Small-50.png", points = 5}
}
local numRows = 3
local numCols = 2
local blockWidth = display.contentCenterX / 2.2
local blockHeight = display.contentCenterY / 2
local row
local col
local imgDataArray = {}
function imagePress (event)
if event.phase == "began" then
local x = event.target.x
local y = event.target.y
event.target:removeSelf()
score_mod.score = score_mod.score + images[event.target.imgNumber].points
function nextImages(x, y)
local nextRandomImg = math.random(1,5)
local nextImage = display.newImage(images[nextRandomImg].name, x, y)
nextImage:addEventListener( "touch", imagePress )
nextImage.imgNumber = nextRandomImg
table.insert(imgDataArray, image)
end
local nextDelay = function() return nextImages(x, y) end
timer.performWithDelay( 2000, nextDelay, 1 )
end
return true
end
function makeImage()
for row = 1, numRows do
for col = 1, numCols do
local x = (col - 1) * blockWidth + 120
local y = (row + 1) * blockHeight - 160
local randomImage = math.random(1, 5)
local image = display.newImage(images[randomImage].name, x, y)
image.imgNumber = randomImage
image.imgX = x
image.imgY = y
image:addEventListener( "touch", imagePress )
table.insert(imgDataArray, image)
end
end
end
makeImage()
end
Game()
Many thanks!
Use score_mod.add(images[event.target.imgNumber].points) instead of score_mod.score = score_mod.score + images[event.target.imgNumber].points

attempt to index upvalue 'letter' error because or arr_str[i]

Im trying to create a random letter answer for my game but the problem is that it wont detect the images please help...
local s = answer --answer
local len = string.len(answer)
local str
local j
for j=1,len do
str = s:sub(j,j)
arr_str[j]= str
end
for i = 1, #arr_str do
local j = math.random( 1,#arr_str)
arr_str[i], arr_str[j] = arr_str[j], arr_str[i]
end
local max = #arr_str
local rowMax = max
--if max > 8 then rowMax = 8 end
local gap = 35
local xPos = halfW - (rowMax/2*35) + 17.5
local yPos = bottom - 20
local startpos = xPos
for i= 1, #arr_str do
slot = display.newImageRect( "images/bg_slot.png", 43, 43 )
slot.x = xPos; slot.y = yPos -40
slotgroup:insert( slot )
slot1 = display.newImageRect( "images/ans_slot.png", 43, 43 )
slot1.x = xPos; slot1.y = yPos
slotgroup:insert( slot1)
print("ERROR",arr_str[i])
letter = display.newImageRect("letters/uc/".. arr_str[i] ..".png",50,50)
letter.name = arr_str[i]
letter.x = xPos; letter.y = yPos
letter.idBg = i
lettergroup:insert( letter)
xPos = xPos + gap
Probably the image that you are trying to open does not exist.
Print the whole path of the image (code below) and look for it on your project folder to make sure you really have that image (pay attention to the file extension as well.
print("letters/uc/".. arr_str[i] ..".png")

in LUA....Corona SDK... My new score is Overlapping without erasing old score?

in code below it displays "SCORE: 100" or whatever however as the score / points change
the totals are over lapping one on top of the other and you cant read them ...
i want the old score erased / removed before displaying new score/points
ANY THOUGHTS HOW TO FIX this...this is LUA and using CORONA SDK
during my test i have sent print statements to try to troubleshoot sections
--Points is being calculated in another location
--UPDATE SCORE POINTS
local function updateScore(Points)
if WTF == 1 then
print ("SCORE: -->",Points)
--PointsText:removeSelf(PointsText)
PointsText = display.newText(Points,0,0,native.sytemFont,42)
PointsText.text = Points
PointsText.xscale = 0.5; PointsText.yscale = 0.5
PointsText:setTextColor(155,155,225)
PointsText.x = centerX * 1
PointsText.y = centerY - 150
ScoreTxt = display.newText("Score: ",0,0,native.systemFont,40)
ScoreTxt:setTextColor(220,50,50)
ScoreTxt.x = display.contentCenterX
ScoreTxt.y = display.contentCenterY-100
end
end
Every time you call updateScore you're creating a new text object. This code ensures you only create the text once.
local function updateScore(Points)
if PointsText == nil then
PointsText = display.newText(Points,0,0,native.sytemFont,42)
end
PointsText.text = Points
PointsText.xscale = 0.5; PointsText.yscale = 0.5
PointsText:setTextColor(155,155,225)
PointsText.x = centerX * 1
PointsText.y = centerY - 150
ScoreTxt = display.newText("Score: ",0,0,native.systemFont,40)
ScoreTxt:setTextColor(220,50,50)
ScoreTxt.x = display.contentCenterX
ScoreTxt.y = display.contentCenterY-100
end
You could also do:
local function updateScore(Points)
if PointsText then
PointsText:removeSelf()
end
PointsText = display.newText(Points,0,0,native.systemFont,42)
PointsText.text = Points
PointsText.xscale = 0.5; PointsText.yscale = 0.5
PointsText:setTextColor(155,155,225)
PointsText.x = centerX * 1
PointsText.y = centerY - 150
ScoreTxt = display.newText("Score: ",0,0,native.systemFont,40)
ScoreTxt:setTextColor(220,50,50)
ScoreTxt.x = display.contentCenterX
ScoreTxt.y = display.contentCenterY-100
end

Resources