简体   繁体   中英

AI help for Corona SDK

I've created a game which loops through a table of properties to create enemies to place on screen. The created enemies are stored in a variable called "baddie", and things like their x and y value are determined by the properties I gave them in the table. Currently, "baddie" creates 3 enemies at varying spots on screen. It looks something like this.

for i=1, #level[section]["enemies"] do
    local object = level[section]["enemies"][i]
    baddie = display.newSprite(baddieSheet, baddieData)
    baddie.anchorX = 0.5
    baddie.anchorY = 1
    baddie.x = object["position"][1]; baddie.y = object["position"][2]; 
    baddie.xScale = -1
    baddie.myName = "Baddie"
    baddie.health = 15
    baddie:setSequence("standrt"); baddie:play()
    physics.addBody(baddie, "dynamic", {radius=22, density=0.1, friction=10.0, bounce=0.0})
    baddie.isFixedRotation = true
    enemyGroup:insert(baddie)
  end

I then inserted all of the created instances stored in the baddie variable, into a display group called "enemyGroup."

Now here's my question. I'm working on my game's AI and storing it all in an enterFrame listener. I want to make a "True/False" flag called "inRange." When the enemy's x position is within 20 pixels of the player's x, inRange = true. When it's true, the enemy will attack him. But I haven't figured out a way to make the inRange flag check for each individual enemy, instead of all of them.

I was thinking of something like,

for i = 1, enemyGroup.numChildren do
   enemyGroup[i].widthBetween = enemyGroup[i].x - sprite.x

   if enemyGroup[i].widthBetween <= 20 and enemyGroup[i].widthBetween >= -20 then
      enemyGroup[i].inRange = true
   else
      enemyGroup[i].inRange = false
   end
   end

But the issue is, enemyGroup[i].inRange is a local value and I can't call for it in outside of the loop or in other functions. This is obviously problematic, because in another function I want to have each individual enemy punch, roll, jump, etc when their individual inRange property is true. Is there a way I can store enemyGroup[i].inRange so that I can call for it whenever?

Sorry if this question is confusing. It's been a struggle to word it.

I'm not sure why this isn't working for you. enemyGroup[i].inRange is not local, its an attribute of the object at enemyGroup[i]. It should be avalble anywhere you can access enemyGroup[i].

Personally I would have not used a display.newGroup() for this, instead I would have just created an array/table that's scoped for the whole scene.

local baddies = {}

then in your loop:

 --enemyGroup:insert(baddie)  instead of this, do this:
 baddies[#baddies + 1] = baddie

Then you have a table that you can loop over, but it's really more code style than functionality. As long as your enemyGroup is scoped at a high enough level that any function that the scene can see.

you should create a file in the structure below:

module(..., package.seeall)

enemyGroup = {}

and in all your files where you want to use this table, first of all require this file( assume you named this file enemies.lua):

local enemiesArray = require "enemies"

-- somewhere in your code:
enemiesArray.enemyGroup[i].isRange = true -- or whatever you like to do

there is one better option for you to use _G variable. when you store an object to _G you can access that wherever you want( like the famous design pattern Singleton ). just set the variable one time and use it anywhere and as much as you want. for example:

-- in one file you set enemy table:
_G.enemies = enemyGroup

-- somewhere else in nowhere :)
print(_G.enemies.isRange)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM