Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Basic Shooter Game — Gideros Forum

Basic Shooter Game

mriicsmriics Member
edited December 2011 in Game & application design
I feel like this shouldn't be extremely hard but I can't seem do figure it out. I am just starting to learn Gideros and wanted to make a game with your player at the bottom and you shoot at the enemies on top. The part that is giving me problems is creating the bullets at the player's location having it travel up and be deleted when it leaves the screen. Any help would be appreciated.

Likes: Paulo777

Dislikes: jmrmbz

+1 -1 (+1 / -1 )Share on Facebook

Comments

  • Hi mriics,

    today I have no time, but tommorow I can show you some code.

    Michael
  • MikeHartMikeHart Guru
    edited December 2011
    Here you go. Not pretty but shows the concept.

    And please load the gtween.lua and the easing.lua from the GTWEEN sample into this project:
     
    --******** The shot class ************
    cShot = gideros.class(Sprite)
     
    function cShot:init()
    	self.shot = Bitmap.new(Texture.new("images/shot.png"))
    	self.shot:setAnchorPoint(0.5, 0.5)
    	stage:addChild(self.shot)
    end
     
     
    function onShotComplete(tween)
    	local child = tween.target
    	local parent = child:getParent()
    	parent:removeChild(child)
    end
     
    function cShot:Fire(x,y)
    	self.shot:setPosition(x,y-0)
    	local tweenx = GTween.new(self.shot, 2, {y = 10}, {onComplete=onShotComplete })
    end
     
    -- Creating and controlling your ship
    local ship = Bitmap.new(Texture.new("images/ship.png"))
    ship:setAnchorPoint(0.5, 0.5)
    ship:setPosition(50,application:getContentHeight()-100)
    stage:addChild(ship)
    GTween.new(ship, 4, {x = application:getContentWidth()-50}, {delay = 0.2, ease = easing.inOutQuadratic, repeatCount = -1, reflect = true})
    ship.counter = 50
     
    function ship:onEnterFrame(event)
    	self.counter = self.counter - 1
    	if self.counter < 0 then
    		self.counter = 50
    		local cs = cShot.new()
    		local x,y = self:getPosition()
    		cs:Fire(x,y-30)
    	end
    end
    ship:addEventListener(Event.ENTER_FRAME, ship.onEnterFrame, ship)

    Likes: atilim, Paulo777

    +1 -1 (+2 / -0 )Share on Facebook
  • Thank you that worked perfectly. I was able to imbed that into my game which controlled the player using the accelerometer and I got that working fine but I can't get the player to stop at the sides of the screen.
  • Just check in the onEnterFrame event against x <0 or x > application:getContentWidth(). If true then set x to 0 or application:getContentWidth().
  • mriicsmriics Member
    edited December 2011
    When I did that the ship will stop at the side but if you hold it against the side for a while it will take a while to start moving again when you tilt the device the other way. If you need my code I can post it.
  • gorkemgorkem Maintainer
    @MikeHart @mriics

    After reading this thread, an idea popped up:

    - A developer writes a sample code and uploads this to a server (e.g dropbox, etc)
    - He then points to this link with a URI, like gdr://dropbox.com/folder/my_sample.gproj in this forum.
    - Mriics clicks on this link in his phone (iPhone or Android)
    - Mobile device recognizes this URI, and immediately runs Gideros Player
    - Gideros Player runs the code.

    This way, Mike doesn't have to put the code in this forum, but rather uploads it on a server, and then writes the link here.

    When Mriics sees the post, but does not have access to his laptop, he can run Mike's sample on his mobile phone immediately.

    Atilim says this can be implemented. We believe it's something "new", and maybe "innovative" since we haven't seen such a use case in any other frameworks out there.

    FYI.
  • @mriics, yes, please post the code or better the project.

    @Gorkem: That woudl only be usefull for one file and no resources, or?
  • Here is the code for the ship's movement

    fx = 0
    fy = 0

    function ShipMove(event)

    local x, y = application:getAccelerometer()
    local shipx, shipy = ship:getPosition()

    fx = x + fx
    fy = y + fy

    shipx = (400 + fy * 400) * -.07
    shipy = 480 - ship:getHeight()

    if shipx < 0 then
    shipx = 0
    end
    if shipx > 700 then
    shipx = 700
    end

    ship:setPosition(shipx, shipy)

    end


    sorry I dont know how to imbed the code in the comment
  • gorkemgorkem Maintainer
    edited December 2011
    @Mike

    There are two possible mehods:

    1) Player first loads the gproj file and all assets on the server mentioned in gproj, like:
    dropbox.com/folder/mysample.gproj
       +-- main.lua
       +-- sounds/
       +-- images/
    2) Player loads the zip file including gproj + all assets in one pack, unzips this and starst playing.


    @mriics

    Use:
    < pre lang="lua">
    your code here
    < / pre>
  • Mmmh, there are some weird things in your code. Like you calculate shipx depending on fy???

    But here is how I would do it:
    function ShipMove(event)
     
        local x, y = application:getAccelerometer()
        local shipx, shipy = ship:getPosition()
     
        shipx = shipx + x
        shipy = 480 - ship:getHeight()
     
        if shipx < 0 then
            shipx = 0
        end
        if shipx > application:getContentWidth() then
            shipx = application:getContentWidth()
        end
     
        ship:setPosition(shipx, shipy)
     
    end
  • Thank you it worked. I had to change one thing however. Where it says
    shipx = shipx + x

    I changed it to
    shipx = shipx - y

    And this worked
    Could this be because it is in landscape.

    Before I changed it, it would move based on the wrong axis.
  • ah, I thought you were in portrait mode. Great that you got it working.

  • Ok, Thanks for your help
  • In 2011.9 version missed "application:getContentWidth"
    Possible it correspond with "Application:getLogicalWidth()"

    p.s. Sorry for bad english.
    Sorry for google translate...
  • application:getcontentWidth is available in beta 7. And yes, you can replace it with getLogicalWidth.
Sign In or Register to comment.