I have a problem I want to move my enemy to random but I don’t know how to fix
–======================================================================–
–== Enemy Class factory
–======================================================================–
local Enemy = class() – define Enemy as a class (notice the capitals)
Enemy.__name = “Enemy” – give the class a name
–======================================================================–
–== Require dependant classes
–======================================================================–
local bulletClass = require (“Classes.Bullet”) – class for the bullet
– as it is the enemy that will create it
–======================================================================–
–== Initialization / Constructor
–======================================================================–
function Enemy:__init(group, xloc, yloc )
self.group = group
self.xloc = xloc
self.yloc = yloc
self.dropAmount = 20
end
–======================================================================–
–== Code / Methods
–======================================================================–
function Enemy:drawEnemy()
self.enemy = display.newImageRect(self.group, “images/enemy.png”, 30, 30 )
self.enemy.id = “enemy”
self.enemy.x = self.xloc
self.enemy.y = self.yloc
physics.addBody( self.enemy, “dynamic” )
self.enemy.collision = function(target, event)
if event.other.id == “leftWall” or event.other.id == “rightWall” then
local options = {
name = “enemyTalk”,
type = “wallCollision”,
wall = event.other.id,
}
Runtime:dispatchEvent(options)
elseif event.other.id == “Player” then
display.remove(self.enemy)
local options = {
name = “gameOver”
}
Runtime:dispatchEvent(options)
end
end
self.enemy:addEventListener(“collision”)
self:desconstuctor()
end
function Enemy:destroy()
local options = {name = “enemyTalk”, type = “dead”}
Runtime:dispatchEvent(options)
display.remove(self.enemy)
self:shoutSound()
end
function Enemy:move(direction, speed)
if direction == “left” then
self.enemy:setLinearVelocity( math.random(60,200), math.random(30,400) )
elseif direction == “right” then
self.enemy:setLinearVelocity(math.random(60,200), math.random(30,400) )
end
end
function Enemy:listen()
function self.enemyTalk (self, event)
if event.action == “move” then
self:move(event.direction, event.speed)
elseif event.action == “drop” then
self.dropTimer = timer.performWithDelay(1, function()
self.enemy.y = self.enemy.y + self.dropAmount
end)
end
end
Runtime:addEventListener(“enemyTalk”, self)
end
function Enemy:desconstuctor()
self.enemy.finalize = function()
if self.dropTimer then
timer.cancel(self.dropTimer)
end
self.enemy:removeEventListener(“collision”)
Runtime:removeEventListener(“enemyTalk”, self)
self.enemy = nil
self = nil
end
self.enemy:addEventListener(“finalize”)
end
–======================================================================–
–== Return factory
–======================================================================–
return Enemy
–======================================================================–
–======================================================================–
–======================================================================–
–== Enemy Class factory
–======================================================================–
local EnemyController = class() – define Enemy as a class (notice the capitals)
EnemyController.__name = “EnemyController” – give the class a name
local EnemyClass = require (“Classes.Enemy”)
–======================================================================–
–== Require dependant classes
–======================================================================–
–======================================================================–
–== Initialization / Constructor
–======================================================================–
function EnemyController:__init(group, direction, numberOfEnemiesAcross, numberOfEnemiesDown )
self.group = group
self.direction = direction
self.numberOfEnemiesAcross = numberOfEnemiesAcross
self.numberOfEnemiesDown = numberOfEnemiesDown
self.speed = 20
self.mostRecentBoundary = nil
self.numberOfStartingEnemies = numberOfEnemiesAcross * numberOfEnemiesDown
self.numberOfCurrentEnemies = self.numberOfStartingEnemies
self.numberOfWallHits = 0
end
–======================================================================–
–== Code / Methods
–======================================================================–
function EnemyController:drawEnemies()
for y = 1,1 do
for x = 1, 5 do
local enemy = EnemyClass:new(self.group, x * 40,y * 40, self.speed)
enemy:drawEnemy()
enemy:listen()
end
end
self:shoutEnemies(“side”)
local options = {name = “enemyTalk”, action = “move”, direction = math.random, speed = self.speed}
Runtime:dispatchEvent(options)
end
function EnemyController:listen()
function self.enemyTalk (self, event)
if event.type == “wallCollision” then
if self.mostRecentBoundary ~= event.wall then
self.mostRecentBoundary=event.wall
self:shoutEnemies(“side”)
self.numberOfWallHits = self.numberOfWallHits + 1
end
end
end
– end
Runtime:addEventListener(“enemyTalk”, self)
end
function EnemyController:shoutEnemies(command)
local action
if command == “side” then
action = “move”
if self.mostRecentBoundary == “leftWall” then
self.direction = “right”
elseif self.mostRecentBoundary == “rightWall” then
self.direction = “left”
end
end
local options = {name = “enemyTalk”, action = action, direction = self.direction, speed = self.speed}
Runtime:dispatchEvent(options)
end
function EnemyController:desconstuctor()
self.enemy.finalize = function()
self.enemy:removeEventListener(“collision”)
Runtime:removeEventListener(“enemyTalk”, self)
self.enemy = nil
self = nil
end
self.enemy:addEventListener(“finalize”)
end
–======================================================================–
–== Return factory
–======================================================================–
return EnemyController