Class Question

I just read the classes tutorial and I’m trying to integrate it into my game. I’m still somewhat unclear on what classes can and cannot do, though. Ideally, I’d like this class to generate an enemy display object and give it a custom physics body when it is called first. Then, I’d like to be able to call the go and change methods for each enemy instance to send them towards the center of the screen and alter them, respectively.

I tried to model my attempt after the tutorial, and it didn’t work. It says I can’t index a boolean variable or something.

Here’s my main code:

[blockcode]
local physics = require(“physics”)
local enemy = require(“enemy”)
require “sprite”
physics.start()
physics.setGravity(0, 0) --no gravity needed
–physics.setDrawMode(“hybrid”)

local enemy1 = enemy.new(“soldier”, “D”)

enemy1.go()[/blockcode]

And here’s my enemy.lua code:

[blockcode]–The required componenets. Not much to see here.
local physics = require(“physics”)
require “sprite”
physics.start()
physics.setGravity(0, 0) --no gravity needed
–Uncomment this to see the physics engine at work
–physics.setDrawMode(“hybrid”)

–[[All purpose srpite sheet containing aminations for soldier, zombie,
and shield enemies walking in the four cardinal directions. Frames are
defined as all 26x25 pixel sections of the sheet from left to right,
then top to bottom]]–
sheet1 = sprite.newSpriteSheet( “enemySheetX1.png”, 26, 25 )

–[[This is a sprite set. It defines the frames used for the different
animations and the sprite sheet used]]–
local all = sprite.newSpriteSet(sheet1, 1, 27) --27 frames

–[[Below are animations made from the sprite sheet
(Sprite set name, “animation name”, starting frame, frame total,
delay in milliseconds between frames, loop count (0 is forever,
-1 is back and forth once, -2 is back and forth forever))]]–
sprite.add( all, “soldierU”, 1, 3, 600, -2 )
sprite.add( all, “soldierD”, 4, 3, 600, -2 )
sprite.add( all, “soldierR”, 7, 3, 600, -2 )
sprite.add( all, “zombieU”, 10, 3, 600, -2 )
sprite.add( all, “zombieD”, 13, 3, 600, -2 )
sprite.add( all, “zombieR”, 16, 3, 600, -2 )
sprite.add( all, “shieldU”, 19, 3, 600, -2 )
sprite.add( all, “shieldD”, 22, 3, 600, -2 )
sprite.add( all, “shieldR”, 25, 3, 600, -2 )

local shapeData = {}

shapeData[“soldierU1”] = {7,-11, 7,-1, -7,-1, -7,-11}
shapeData[“soldierU2”] = {7,-1, 10,4, 7,8, -7,8, -10,4, -7,-1}
shapeData[“soldierU3”] = {7,8, 7,12, -7,12, -7,8}
shapeData[“zombieU1”] = shapeData[“soldierU1”]
shapeData[“zombieU2”] = shapeData[“soldierU2”]
shapeData[“zombieU3”] = shapeData[“soldierU3”]
shapeData[“shieldU1”] = {-10,-11, 10,-11, 10,12, -10,12}
shapeData[“soldierD1”] = shapeData[“soldierU1”]
shapeData[“soldierD2”] = shapeData[“soldierU2”]
shapeData[“soldierD3”] = shapeData[“soldierU3”]
shapeData[“zombieD1”] = shapeData[“soldierU1”]
shapeData[“zombieD2”] = shapeData[“soldierU2”]
shapeData[“zombieD3”] = shapeData[“soldierU3”]
shapeData[“shieldD1”] = shapeData[“shieldU1”]
shapeData[“soldierR1”] = {3,-11, 7,-8, 7,3, -8,3, -8,-8, -4,-11}
shapeData[“soldierR2”] = {7,3, 8,5, 7,8, -8,9, -9,6, -8,3}
shapeData[“soldierR3”] = {8,8, 5,11, -5,11, -8,9}
shapeData[“zombieR1”] = shapeData[“soldierR1”]
shapeData[“zombieR2”] = shapeData[“soldierR2”]
shapeData[“zombieR3”] = shapeData[“soldierR3”]
shapeData[“shieldR1”] = shapeData[“shieldU1”]

–[[“nametag” is the first part of the data identifier which tells
Corona what animation to use and what shape the object should
be when calculating collisions. “direction” is the second part
of that identifier. “nametag” specifes the type of enemy to use,
and “direction” specifies the direction the animation will face]]–

local enemy = {}
local enemy_mt = { __index = enemy }
local nametag = nil
local direction = nil
local xCor = nil
local yCor = nil

function enemy.new( name, dir, xx, yy) – constructor
print (“marker 3”)
if xx ~= nil and yy ~= nil then
dummyX = xx
dummyY = yy
elseif direction == “D” then
dummyX = math.random (-60, (display.viewableContentWidth/2 + (display.viewableContentWidth/2)*0.1) * 2.2)
dummyY = -10
elseif direction == “U” then
dummyX = math.random (-60, (display.viewableContentWidth/2 + (display.viewableContentWidth/2)*0.1) * 2.2)
dummyY = (display.viewableContentHeight * 1.2)
else
–R is assumed. left or right chosen randomly
choice = math.random(2)
if choice == 1 then
dummyX = -60
dummyY = math.random (-20, (display.viewableContentHeight * 1.3))
else
dummyX = (display.viewableContentWidth * 1.2)
dummyY = math.random (-20, (display.viewableContentHeight * 1.3))
end
end

local newEnemy = {
nametag = name,
direction = dir,
xCor = dummyX,
yCor = dummyY
}
return setmetatable( newEnemy, enemy_mt )
end

function enemy:go()
local enemyObj = sprite.newSprite( all )
local enemyObj2 = nil
enemyObj.x = xCor
enemyObj.y = yCor
if shapeData[nametag…direction…3] ~= nil then
physics.addBody (enemyObj, {isSensor = true, shape = shapeData[nametag…direction…1]},
{isSensor = true, shape = shapeData[nametag…direction…2]},
{isSensor = true, shape = shapeData[nametag…direction…3]})
elseif shapeData[nametag…direction…2] ~= nil then
physics.addBody (enemyObj, {isSensor = true, shape = shapeData[nametag…direction…1]},
{isSensor = true, shape = shapeData[nametag…direction…2]})
else
physics.addBody (enemyObj, {isSensor = true, shape = shapeData[nametag…direction…1]})
end
if xCor > (display.viewableContentWidth/2) then
enemyObj.xScale = -1
end
enemyObj:setLinearVelocity( (-(enemyObj.x - (display.viewableContentWidth/2))) * 150 / (500), (-(enemyObj.y - (display.viewableContentHeight/2)))* 150 / (500) )
enemyObj:prepare(nametag…direction)
enemyObj:play() --plays animation
end

function enemy:change()
nametag2 = “soldier”
enemyObj2 = sprite.newSprite( all )
enemyObj2.x = enemyObj.x
enemyObj2.y = enemyObj.y
if shapeData[nametag2…direction…3] ~= nil then
physics.addBody (enemyObj2, {isSensor = true, shape = shapeData[nametag2…direction…1]},
{isSensor = true, shape = shapeData[nametag2…direction…2]},
{isSensor = true, shape = shapeData[nametag2…direction…3]})
elseif shapeData[nametag2…direction…2] ~= nil then
physics.addBody (enemyObj2, {isSensor = true, shape = shapeData[nametag2…direction…1]},
{isSensor = true, shape = shapeData[nametag2…direction…2]})
else
physics.addBody (enemyObj2, {isSensor = true, shape = shapeData[nametag2…direction…1]})
end
enemyObj2:prepare(nametag2…direction)
enemyObj:removeSelf()
enemyObj2.isVisible = true
enemyObj2.isBodyActive = true
enemyObj2:play() --plays animation
end[/blockcode]

What should I do? [import]uid: 89724 topic_id: 17250 reply_id: 317250[/import]

Bump. [import]uid: 89724 topic_id: 17250 reply_id: 65333[/import]

This is too long for most people to go over and pick apart and cannot be tested at all because it requires your images for the sprites.

That said, are you using;
[lua]module(…, package.seeall)[/lua]
at the start of the enemy.lua file?

Peach :slight_smile: [import]uid: 52491 topic_id: 17250 reply_id: 65370[/import]

I wasn’t, but that’s because I didn’t see it used in this tutorial:

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/ [import]uid: 89724 topic_id: 17250 reply_id: 65580[/import]

OK, well, this;

I tried to model my attempt after the tutorial, and it didn’t work. It says I can’t index a boolean variable or something.

Your code is over 150 lines - was a line mentioned in the error? What line was it? Have you tried to isolate the section of your code causing the error? [import]uid: 52491 topic_id: 17250 reply_id: 65593[/import]

What you could do is, for example, lets say you are having trouble making a shield. You could comment out the shield part of the code, and line-by-line uncomment it to see if you can figure out which line is defective.

P.S. be sure to check the console for a line number before you go to all that trouble though LOL

Good luck,
J.K. [import]uid: 66117 topic_id: 17250 reply_id: 65597[/import]

You don’t return the enemy table at the end. [import]uid: 70391 topic_id: 17250 reply_id: 65601[/import]

@ Peach:

The line was line 8, the calling of enemy.new in the main.lua file.

@ Joe

I’m not sure what’s causing the error, but I’m fairly certain it has something to do with how I’m implementing my methods. I’ve never been told I was indexing a boolean value value before. It says “attempt to index local ‘enemy’ (a boolean value).”

@ Green

That did it! Thanks everyone! I appreciate your patience in helping me learn this. [import]uid: 89724 topic_id: 17250 reply_id: 65662[/import]

Glad to know its working. The game looks exciting, can’t wait to see it in the app store/android market!

J.K. [import]uid: 66117 topic_id: 17250 reply_id: 65664[/import]