简体   繁体   English

针对Corona SDK的AI帮助

[英]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. 创建的敌人存储在一个名为“ baddie”的变量中,它们的x和y值由我在表中为其提供的属性确定。 Currently, "baddie" creates 3 enemies at varying spots on screen. 目前,“ baddie”会在屏幕上的不同位置产生3个敌人。 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." 然后,我将存储在baddie变量中的所有已创建实例插入到名为“ enemyGroup”的显示组中。

Now here's my question. 现在这是我的问题。 I'm working on my game's AI and storing it all in an enterFrame listener. 我正在开发游戏的AI,并将其全部存储在enterFrame侦听器中。 I want to make a "True/False" flag called "inRange." 我要创建一个名为“ inRange”的“ True / False”标志。 When the enemy's x position is within 20 pixels of the player's x, inRange = true. 当敌人的x位置在玩家x的20像素以内时,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. 但我还没有想出一个办法,使每一个人的敌人INRANGE标志检查,而不是全部。

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. 但是问题是,敌人组[i] .inRange是一个局部值,我不能在循环外或其他函数中调用它。 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. 这显然是有问题的,因为在另一个函数中,当每个敌人的inRange属性为true时,我想让每个敌人对其进行猛击,滚动,跳跃等操作。 Is there a way I can store enemyGroup[i].inRange so that I can call for it whenever? 有没有一种方法可以存储敌人组[i] .inRange,以便随时可以调用它?

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]. 敌人组[i] .inRange不是本地的,它是敌人组[i]上对象的一个​​属性。 It should be avalble anywhere you can access enemyGroup[i]. 您可以访问敌人组[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. 就我个人而言,我不会为此使用display.newGroup(),而是会创建一个针对整个场景的数组/表。

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): 在要使用此表的所有文件中,首先需要此文件(假设您将此文件命名为敌人.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. 有一个更好的选择供您使用_G变量。 when you store an object to _G you can access that wherever you want( like the famous design pattern Singleton ). 当您将对象存储到_G您可以随时随地访问该对象(例如著名的设计模式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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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