Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to do a turret machine gun? — Gideros Forum

How to do a turret machine gun?

yaxxaryaxxar Member
edited October 2015 in General questions
Hello folks, I m on the start of my new game, is based on a tower defense game with a twist of shooter game, my idea is the player can rotate a turret and the turret fires like a machine gun, I read some code from another languages but I m running out of ideas how to make that on Gideros, I know I need to do with an array, but the creation and update of the bullets is a hard thing for me at the moment, do someone have an example? please :o3 thanks!

Comments

  • piepie Member
    edited October 2015 Accepted Answer
    I would use a Timer or enterframe event to know when it's time to fire again:
    you need a place (the bullets array) to store references to the bullets on screen, so that you can interact again with each bullet (for example just to get them back into a pool, or to make them moving, depending on your code) then:
     
    bullets = {}
    counter = 1 --this is to give a unique id to the newly created bullet
     
    local function createBullet()
     bx,by=turret:getPosition() --this is to get the turret position as a start x,y for the bullet
     
     bullets[counter] = bullet.new(turret.direction) --create a new bullet instance (or draw it out from pool) named as a number (counter), I assume that you have a property in the  turret to know the direction it is pointing to
     
     stage:addChild(bullets[counter]) --if you're adding the bullets to stage
     bullets[counter]:setPosition(bx,by) 
     
     counter = counter+1 --increase the counter for the next bullet
    end
    This is a mod version of another example, I didn't test it but should give you something to start from :)



    Likes: yaxxar

    +1 -1 (+1 / -0 )Share on Facebook
  • Tom2012Tom2012 Guru
    edited October 2015 Accepted Answer
    Hi @yaxxar

    I've got a turret in my game. I have a turret class and a bullet class.

    I'd better attach the files as the code is a bit long to post here...

    My turret might have stuff you don't need but I'm sure you'll see how it works. (I'm really basic at programming!) It rotates, sleeps a bit, then starts up again. But the bullets being fired out will be the same for any kind of turret game. (Trig etc...)

    It's in level 7 of my game if you want to see it in action. Although you'll have to play the 6 stages before to see it lol sorry

    Good luck and ask if there's anything I can do to help!
    lua
    lua
    Turret.lua
    9K
    lua
    lua
    Bullet.lua
    4K

    Likes: yaxxar

    +1 -1 (+1 / -0 )Share on Facebook
  • I just try a lot of things, but I can t accomplish the logic to fire my turret, on this case I use a monster sprite as turret, :( I let my project for checking out
    zip
    zip
    Tureto.zip
    13K
  • piepie Member
    Accepted Answer
    just had time for a quick look, I noticed 2 things but couldn't test right now

    1) you need to put a limit in enterframe, otherwise in 1 second you shoot 60 bullets :) you can use frameCount or a counter you set up.

    2) when you create the bullet, I think it's better if you give it a name already, you risk that bulletIndex is updated before createBullet could get it.
    local function onEnterFrame(event)
    	chuhulu:set("rotation",grados)
    	--fireBullet()
    	if event.frameCount%application:getFps()==0 then --every second do:
    		event:stopPropagation()
     
    			if (recoilIndex == 10) then
    				if (bulletIndex <= maxBullets) then
    					createBullet(bulletIndex)
    end
    end
    end
    end
    ...

    Likes: yaxxar

    +1 -1 (+1 / -0 )Share on Facebook
  • @yaxxar I'll have a check of your file as soon as I get home from work. :)
  • Tom2012Tom2012 Guru
    edited October 2015 Accepted Answer
    Hi @yaxxar

    I had a look at your project file and played around with the code. I've attached the edited version to this post.

    I think what you're trying to do is create a 'pool' of bullets, which are used over and over to avoid creating and removing lots of bullets?

    If I were you, I'd just let your game create the bullets and remove them when they leave the 'edge' of the screen, or hit a target etc.
    if (self.x < -10 or self.x > 510 or self.y < -10) then
    		self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)	
    		stage:removeChild(self)
    		print("killed")
    	end
    It's simpler and you should not have any memory leaks. I've also added the memory checker to your project (which can be removed or disabled in main.lua) which shows the bullets are added and removed from memory.

    Hope this helps and good luck!

    zip
    zip
    Tureto.zip
    17K

    Likes: yaxxar

    +1 -1 (+1 / -0 )Share on Facebook
  • @pie and @Tom2012 thanks a lot for the help I will test all the codes after work, you are awesome guys! thanks \m/ :-bd
  • @pie and @Tom2012 sorry for the delayed answer I tested and now my turret works very good just what I need to start my game! Thanks a lot guys!
  • Very cool! Look forward to seeing the game progress.
Sign In or Register to comment.