Tuto Gideros Game Template1 Part 8 Player

From GiderosMobile
Revision as of 20:01, 27 October 2024 by MoKaLux (talk | contribs) (Created page with "__TOC__ == a Player == We test our Gideros Game Template1 and add a player you can control with the keyboard. '''Note: this is for testing purposes only''' I didn't really...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

a Player

We test our Gideros Game Template1 and add a player you can control with the keyboard.

Note: this is for testing purposes only

I didn't really know where to put the code so I chose to create a file called "player1.lua" in the gfx folder.

The Player Class will mainly consist of:

  • a Sprite (a Pixel)
  • some vars
  • controlling the player using the keyboard

The code:

Player1 = Core.class(Sprite)

function Player1:init()
	self.sprite = Pixel.new(math.random(0xffffff), 1, 32, 64)
	self.sprite:setAnchorPoint(0.5, 0.5)
	-- some vars
	self.x = 0
	self.y = 0
	self.speed = 32*6
	self.vx = 0
	self.vy = 0
	self.sx = 1
	self.sy = 1
	self.w = self.sprite:getWidth() * self.sx
	self.h = self.sprite:getHeight() * self.sy
	self.flip = 1
	-- position
	self.sprite:setPosition(self.x, self.y)
	-- order
	self:addChild(self.sprite)
	-- let's go
	self:myKeysPressed()
end

-- keys handler
function Player1:myKeysPressed()
	self:addEventListener(Event.KEY_DOWN, function(e)
		-- movements
		if e.keyCode == KeyCode.LEFT or e.keyCode == g_keyleft then self.isleft = true end
		if e.keyCode == KeyCode.RIGHT or e.keyCode == g_keyright then self.isright = true end
		if e.keyCode == KeyCode.UP or e.keyCode == g_keyup then self.isup = true end
		if e.keyCode == KeyCode.DOWN or e.keyCode == g_keydown then self.isdown = true end
		-- actions
		if e.keyCode == g_keyaction1 then self.isactionpunch1 = true end
		if e.keyCode == g_keyaction2 then self.isactionpunch2 = true end
		if e.keyCode == g_keyaction3 then self.isactionpunch3 = true end
	end)
	self:addEventListener(Event.KEY_UP, function(e)
		-- movements
		if e.keyCode == KeyCode.LEFT or e.keyCode == g_keyleft then self.isleft = false end
		if e.keyCode == KeyCode.RIGHT or e.keyCode == g_keyright then self.isright = false end
		if e.keyCode == KeyCode.UP or e.keyCode == g_keyup then self.isup = false end
		if e.keyCode == KeyCode.DOWN or e.keyCode == g_keydown then self.isdown = false end
		-- actions
		if e.keyCode == g_keyaction1 then self.isactionpunch1 = false end
		if e.keyCode == g_keyaction2 then self.isactionpunch2 = false end
		if e.keyCode == g_keyaction3 then self.isactionpunch3 = false end
	end)
end

Code comments

In init we make our player1 be a Pixel.

We set various variables which we will update in the Game scene (LevelX) to reflect the user input.

keys handler

This is where the key remapping system shines :-)

Next?

Well that's it for this Gideros Game Template1 tutorial.

This is now yours to use/modify (add scenes, ...).


I hope this tutorial was useful and will help you on your journey making great games using Gideros Studio ;-)

the Project file

Media:GIDEROS_GAME_TEMPLATE1.zip (tip: right click and save link as)


Prev.: Tuto Gideros Game Template1 Part 7 Game

END


Tutorial - Gideros Game Template1