class Enemy
def initialize
...
@delay=0 #initialize the variable
end
def update
...
@delay-=1 #decrease the variable so delay isn't infinite
end
def shoot
return if @delay > 0 #don't shoot if delay is not finished
bullet = @bullets.find { |bullet| not bullet.shot }
if bullet
@delay = 60 #delay shot for 60 game ticks (one second)
bullet.shot = true
bullet.sample.play
end
end
...
end
(enemy.x - @player.x).abs
instead.
bullet.shot=true
, which only makes bullet updated and drawn. But to make your enemy actually shoot the bullet, you need to set it's initial position (probably at enemy's "shootpoint"). As you made currently, the bullet appears "somewhere" and you can't see it.
Array.new(5) { Bullet.new(enemy, window) }
It creates array filled with the same bullet. So your enemy shoots the same bullet every time and he won't shoot when this bullet is still of the screen. You have to make array of five instances of bullets.
@bullets = [ Bullet.new(self, window), Bullet.new(self, window), Bullet.new(self, window), Bullet.new(self, window), Bullet.new(self, window) ]
@bullets = []
5.times{ @bullets << Bullet.new(self, window) }
irb(main):005:0> bullets = Array.new(5) { Numeric.new }
=> [#<Numeric:0x2cdaf38>, #<Numeric:0x2cdaf20>, #<Numeric:0x2cdaf08>, #<Numeric:0x2cdaef0>, #<Numeric:0x2cdaed8>]
@enemies.each { |enemy|
@enemy_bullets = Array.new(5) { Bullet.new(enemy, window) }
enemy.load_bullets(@enemy_bullets)
}
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill