【问题标题】:Bullet Fire Reset Event Corona SDKBullet Fire 重置事件 Corona SDK
【发布时间】:2013-05-07 20:28:12
【问题描述】:

所以我想做的是让敌人随机开火,但是当子弹离开屏幕时向敌人发出信号并让敌人再次开火。每个敌人实例在任何时候都只能有一个活动的子弹实例。到目前为止,我只是在测试 fire 和 refire 的实现。调用 Enemy 实例的函数 shoot 如下:

function enemy:shoot()
    --Calls Bullet Obj file
    local Bullet = require "Bullet";

    --Movement time per space
    local DEFAULTTIME = 5;

    --checking if property, there currently is an active bullet instance in scene
    if self.activeBul ==false then
          --move the bullet
          self.bullet:trajectBullet({x=self.sprite.x,y=display.contentHeight, time = DEFAULTTIME*   (display.contentHeight-self.sprite.y)});

          --there is now an active Bullet linked to this enemy
          self.activeBul = true;

    else

    end
end

现在在 trajectBullet 中发生的只是移动实现。我现在想弄清楚如何让链接的敌人实例知道它的子弹不在屏幕上。我对 lua 和 Corona SDK 还很陌生,所以我仍然掌握如何最好地处理事情,所以请耐心等待我在下面寻找什么的简单概述

--random enemy fire
 --move Bullet location on top of enemy(appears enemy fires)
 --makes linked bullet visible
 --simulates trajectory
  CASE:* doesn't hit anything and goes off screen*
   --hides Bullet
   --signals to linked enemy it can fire again(changing activeBul to false)

要记住几件事,我将 Bullet 和 Enemy 作为元表。 Bullet 实例也是在创建敌人的同时创建的。所以敌人永远不会创建多个 Bullet 实例,只是隐藏并重新定位它开火。

我真的在寻找关于我应该如何让它正常工作的见解,任何建议都将不胜感激

【问题讨论】:

  • 你不应该在敌人开枪时require-ing Bullet.lua 文件。敌人元表应包含对 Bullet 类的引用,每个实例都利用该类。

标签: sdk lua coronasdk


【解决方案1】:

如果您已经知道子弹何时离开屏幕,您可以使用两种方法: 1. 对敌人的子弹有一个参考,所以你可以在它上面调用一个函数来“recicle”子弹; 2. 创建一个自定义事件,在子弹离开屏幕时调度。

由于灵活性,我会坚持使用 2)。

您可以在此处找到更多信息:


-- 1)
function enemy:shoot()
  local Bullet = require "Bullet";
  Bullet.owner = self;
  -- the same as your previous code
end;

function enemy:canShootAgain()
  self.activeBul = false;
end;

-- When you want the enemy to shoot again, just call canShootAgain from bullet
bullet.owner:canShootAgain();

-- 2)
function enemy:shoot()
  local Bullet = require "Bullet";
  Bullet.owner = self;
  -- the same as your previous code
end;

function enemy:canShootAgain()
  self.activeBul = false;
end;

-- When you want the enemy to shoot again, just call canShootAgain from bullet
bullet.owner:dispatchEvent({name = 'canShootAgain'});

-- Add this to the end of your enemy construction
enemy:addEventListener('canShootAgain', enemy);

【讨论】:

  • 好的,我试图做 2) 就像你提到的那样,但是我在调​​整事件本身的 activeBul 属性时遇到了问题,并且能够简单地调用一个函数来更改 activeBul 的属性适当的例子。我想问题的要点是我在尝试调整的对象和触发的事件之间进行通信时遇到问题。
  • 嗨。我添加了一些示例代码来举例说明如何实现这两种方式。
  • 非常感谢提供的示例,我现在将尝试实施这些示例!!
猜你喜欢
  • 2013-04-01
  • 2012-02-26
  • 1970-01-01
  • 2014-10-19
  • 2014-01-07
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多