2
votes

First time post and I sincerely apologize if this has been answered in any other post but I have not been able to find a resolution for the problem I'm facing on this or any other site. I'm a new programmer self-teaching with web tutorials and any other resource I have found. I am trying to create code which will spawn characters and allow you to call them. I've had trouble assigning an index value to the individual instances I have created with a for function. I have tried to establish the instance as both a table and a group display object. If anyone is able to point me in the direction of any resources to get a more indepth understanding of tables and group display objects for the Corona SDK implementation of Lua I'm sure that my problem is that I don't have a thorough enough understanding of these particular functionalities and how they work. Here is the code I've written so far.

-- Character Game

require "sprite"

require "ui"

local background = display.newImage("Background Placeholder.png") 

halfW = display.viewableContentWidth / 2
halfH = display.viewableContentHeight / 2

local numCharacters = 20

local roundedRect = display.newRoundedRect( 365, 20, 110, 40, 8 )
roundedRect:setFillColor( 0, 255, 0, 80 )

score = 0

t = ui.newLabel{ bounds = { 370, 30, 100, 40 },
text = "Score " ..  score,
textColor = { 255, 0, 20, 255 },
size = 18,
align = "center"
}

local scoreboard = function ( event )
    t:setText( "Score " .. score )
end

Runtime:addEventListener( "enterFrame", scoreboard )


local group = display.newGroup()

local character = sprite.newSpriteSheetFromData( "Character Placeholder.png", require("Character Placeholder").getSpriteSheetData() )

local characterSet1 = sprite.newSpriteSet(character,1,8)

sprite.add(characterSet1,"character",1,8,1500,0)

local characterplay = function( event )
    score = score + group.points
    group[i]:removeSelf()
end

do  
    for i=1, numCharacters do
        group:insert(sprite.newSprite(characterSet1))
        group[i].xScale = .2
        group[i].yScale = .2
        group.points = 50
        group[i]:setReferencePoint ( display.BottomCenterReferencePoint )
        group[i]:translate( halfW + math.random( -100, 100 ), halfH + math.random( -130, -110 ) )
    end 

    timer.performWithDelay( 500, charactermovie )

    for i=1, 21 do
        timer.performWithDelay( math.random( 500, 5000 ) , charactermove )

        charactermove = function(event )
            transition.to( group[i], { time=10000, y = 580 } )
            transition.to( group[i], { time=8000, x = math.random( 0, 480 ) } )
            transition.to( group[i], { time=7000, xScale = 1.5} )
            transition.to( group[i], { time=7000, yScale = 1.5} )
            group[i]:prepare("character")
            group[i]:play()
        end

        group[i]:addEventListener( "tap", characterplay )
    end
end




charactermovie = function( event )
group[i]:prepare("character")
group[i]:play()
end

local function spriteListener( event )
    print( "Sprite event: ", event.sprite, event.sprite.sequence, event.phase )
end

for i, group in pairs(group) do print (group, i, v) end

I'm currently getting an "attempt to index field '?' at the line containing this code "group[i]:addEventListener( "tap", zombieplay )" upon launch and a "nil key supplied for property lookup" error at the "group[i]:removeSelf()" line of the "zombieplay" function. I've tried moving the offending code to a variety of locations to see if this is a scoping issue but I largely run into the same error and believe I may not properly understand indexes and keys... I've found that the app functions as intended but I have to call index keys 1 through 21 to get them all to move even though I am only calling for 20 characters and the removeSelf line throwing errors is not removing the individual characters. I'm going to try writing a module for the characters and see if that helps clear any of this up. I will post my results shortly.

1
you should indent your code so we can read it easily - cctan
Thanks I will work on cleaning it up shortly. - zombie-droid
This has been properly formatted now. If anyone sees what I'm doing wrong here it will be greatly appreciated! I tried to set this up in a module and I've run into basically all the same problems so I'm going to try to stick with it getting it to work this way. This is basically what would go into the module anyway... Thanks in advance to anyone who checks this out!!!! - zombie-droid

1 Answers

0
votes

Your characterplay and charactermovie functions are trying to use the variable i, this is outside the functions scope.

There is a property of event called target, which is used to get the event callee. You want to do something like this:

local characterplay = function( event )
    score = score + group.points
    event.target:removeSelf()
end