Difference between revisions of "Animated Sprite Factory"

From GiderosMobile
(Created page with "__TOC__ <languages /> <translate>Here you will find various resources to help you create games and apps in Gideros Studio.</translate> <br/> <translate>'''note''': You may hav...")
 
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__TOC__
 
__TOC__
<languages />
+
Here you will find various resources to help you create games and apps in Gideros Studio.
<translate>Here you will find various resources to help you create games and apps in Gideros Studio.</translate>
 
<br/>
 
<translate>'''note''': You may have to provide your own assets (fonts, gfx, …).</translate>
 
<br/>
 
  
=== <translate>Description</translate> ===
+
'''note''': You may have to provide your own assets (fonts, gfx, ).</br>
This section deals with animating any of your sprites (player, enemies, collectibles, ...). It can be used either in conjunction with cbump, liquidfun or as a standalone class.
+
'''note 2''': You can copy/paste the code :-)
  
=== <translate>Animation Sprite Factory (player, enemies, collectibles, npcs, ...)</translate> ===
+
=== Description ===
This animation sprite factory class allows you to control all your characters parameters like speed, animations, ...
+
This section deals with animating any of your sprites (player, enemies, collectibles, ...). It can be used either in conjunction with cbump, liquidfun(box2d) or as standalone.
<source lang="lua">
 
Sprite_Maker = Core.class(Sprite)
 
  
function Sprite_Maker:init(xname, xspritesheet, xcols, xrows, xposx, xposy)
+
=== Spritesheet Animations (player, enemies, collectibles, npcs, ...) ===
-- settings
+
This is an example of how to animate using a spritesheet.
self.name = xname
 
self.x = xposx
 
self.y = xposy
 
self.vx = 0
 
self.vy = 0
 
self.flip = 1
 
self.isonfloor = false
 
self.isattacking = false
 
self.lives = 1
 
self.accel = 500
 
self.maxspeed = 200 -- 150 -- 200
 
self.jumpspeed = GRAVITY / 1.5
 
  
-- animations
+
==== If you are using a class ====
 +
<syntaxhighlight lang="lua">
 +
Player = Core.class(Sprite)
 +
 
 +
function Player:init()
 +
-- spritesheet animations
 +
local spritesheettex = Texture.new("gfx/playable/mr_man.png")
 +
local cols, rows = 8, 6
 +
self.spritesheetimgs = {} -- table that will hold all the images in the sprite sheet
 +
self.anims = {} -- table that will hold all our animations ("idle", "run", ...)
 
self.currentanim = ""
 
self.currentanim = ""
 
self.frame = 0
 
self.frame = 0
self.animspeed = 1 / 8
+
self.animspeed = 1 / 7
 
self.animtimer = self.animspeed
 
self.animtimer = self.animspeed
 
+
-- 1 retrieve all anims in spritesheet
-- retrieve all anims in texture
+
local w, h = spritesheettex:getWidth() / cols, spritesheettex:getHeight() / rows
local myanimstex = Texture.new(xspritesheet)
+
local spritesheettexregion = nil
local cellw = myanimstex:getWidth() / xcols
+
for r = 1, rows do
local cellh = myanimstex:getHeight() / xrows
+
for c = 1, cols do
local myanims_list = {}
+
spritesheettexregion = TextureRegion.new(spritesheettex, (c - 1) * w, (r - 1) * h, w, h)
for r = 1, xrows do
+
self.spritesheetimgs[#self.spritesheetimgs + 1] = spritesheettexregion
for c = 1, xcols do
 
local myanimstexregion = TextureRegion.new(
 
myanimstex, (c - 1) * cellw, (r - 1) * cellh, cellw, cellh)
 
myanims_list[#myanims_list + 1] = myanimstexregion
 
 
end
 
end
 
end
 
end
 +
-- 2 create animations
 +
self:createAnim("idle", 1, 4)
 +
self:createAnim("run", 9, 14)
 +
self.currentanim = "run"
 +
-- 3 we can now create the player
 +
self.bitmap = Bitmap.new(self.spritesheetimgs[1])
 +
self.bitmap:setAnchorPoint(0.5, 0.5)
 +
self.bitmap:setScale(2)
 +
self:addChild(self.bitmap)
 +
-- LISTENERS
 +
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
 +
end
  
-- create anims
+
-- PLAYER LOOP
self.anims = {}
+
function Player:onEnterFrame(e)
if self.name == "player1" then
+
-- animations
self:createAnim("idle", 1, 2, myanims_list)
+
if self.currentanim ~= "" then
self:createAnim("jump_up", 38, 38, myanims_list)
+
self.animtimer = self.animtimer - e.deltaTime
self:createAnim("jump_down", 39, 39, myanims_list)
+
if self.animtimer <= 0 then
self:createAnim("walk", 13, 16, myanims_list)
+
self.frame += 1
self:createAnim("shoot", 67, 70, myanims_list)
+
self.animtimer = self.animspeed
elseif self.name == "enemy01" then
+
if self.frame > #self.anims[self.currentanim] then
self:createAnim("walk", 1, 7, myanims_list)
+
self.frame = 1
elseif self.name == "enemy02" then
+
end
self:createAnim("walk", 1, 5, myanims_list)
+
self.bitmap:setTextureRegion(self.anims[self.currentanim][self.frame])
elseif self.name == "coin01" then
+
end
self:createAnim("walk", 1, 6, myanims_list)
 
 
end
 
end
 +
self.bitmap:setPosition(128, 64)
 +
end
  
-- the bitmap
+
-- FUNCTIONS
self.bmp = Bitmap.new(myanims_list[1]) -- starting bmp texture
+
function Player:createAnim(xanimname, xstart, xfinish)
self.bmp:setAnchorPoint(0.5, 0.5)
+
self.anims[xanimname] = {}
if self.name == "player1" then
+
for i = xstart, xfinish do
self.w = self.bmp:getWidth() / 2
+
self.anims[xanimname][#self.anims[xanimname] + 1] = self.spritesheetimgs[i]
self.h = self.bmp:getHeight() - 0
 
-- set position inside sprite
 
self.bmp:setPosition(self.w / 2, self.h / 2 - 0 / 2)
 
elseif self.name == "enemy01" then
 
self.w = self.bmp:getWidth() / 2
 
self.h = self.bmp:getHeight() - 0
 
-- set position inside sprite
 
self.bmp:setPosition(self.w / 2, self.h / 2 - 0 / 2)
 
elseif self.name == "enemy02" then
 
self.w = self.bmp:getWidth() / 8
 
self.h = self.bmp:getHeight() - 0
 
-- set position inside sprite
 
self.bmp:setPosition(self.w / 2, self.h / 2 - 0 / 2)
 
else
 
self.w = self.bmp:getWidth()
 
self.h = self.bmp:getHeight()
 
-- set position inside sprite
 
self.bmp:setPosition(self.w / 2, self.h / 2)
 
 
end
 
end
 +
end
 +
</syntaxhighlight>
  
-- collisions debugging
+
==== If you are not using a class ====
local mypixel = Pixel.new(0xff0000, 0.5, self.w, self.h)
+
<syntaxhighlight lang="lua">
 +
-- spritesheet animations
 +
local spritesheettex = Texture.new("gfx/hero/HQ_Trooper_all.png") -- change accordingly
 +
local cols, rows = 6, 13 -- change accordingly
 +
local spritesheetimgs = {} -- table that will hold all the images in the sprite sheet
 +
local anims = {} -- table that will hold all our animations ("idle", "run", ...)
 +
local currentanim = ""
 +
local frame = 0
 +
local animspeed = 1 / 7 -- change accordingly
 +
local animtimer = animspeed
  
-- our sprite is ready
+
-- 1 retrieve all anims in spritesheet
local mysprite = Sprite.new()
+
local w, h = spritesheettex:getWidth() / cols, spritesheettex:getHeight() / rows
mysprite:addChild(self.bmp)
+
local spritesheettexregion = nil
-- mysprite:addChild(mypixel) -- debug
+
for r = 1, rows do
self:addChild(mysprite)
+
for c = 1, cols do
 
+
spritesheettexregion = TextureRegion.new(spritesheettex, (c - 1) * w, (r - 1) * h, w, h)
-- hero controls
+
spritesheetimgs[#spritesheetimgs + 1] = spritesheettexregion
if self.name == "player1" then
 
self.iskeyleft = false
 
self.iskeyright = false
 
self.iskeyup = false
 
self.iskeydown = false
 
self.iskeyspace = false
 
 
 
self:addEventListener(Event.KEY_DOWN, self.onKeyDown, self)
 
self:addEventListener(Event.KEY_UP, self.onKeyUp, self)
 
 
end
 
end
 
end
 
end
  
-- FUNCTIONS
+
-- 2 create animations
function Sprite_Maker:createAnim(xanimname, xstart, xfinish, xanimslist)
+
function createAnim(xanimname, xstart, xfinish)
self.anims[xanimname] = {}
+
anims[xanimname] = {}
 
for i = xstart, xfinish do
 
for i = xstart, xfinish do
self.anims[xanimname][#self.anims[xanimname] + 1] = xanimslist[i]
+
anims[xanimname][#anims[xanimname] + 1] = spritesheetimgs[i]
 
end
 
end
 
end
 
end
 +
createAnim("idle", 1, 2)
 +
createAnim("run", 25, 30)
 +
 +
-- 3 we can now create the player
 +
local bitmap = Bitmap.new(spritesheetimgs[1])
 +
bitmap:setAnchorPoint(0.5, 0.5)
 +
bitmap:setPosition(128, 64)
 +
stage:addChild(bitmap)
 +
currentanim = "idle"
 +
--currentanim = "run"
  
-- KEYS HANDLER
+
-- GAME LOOP
function Sprite_Maker:onKeyDown(e)
+
function onEnterFrame(e)
-- keys pressed
+
if currentanim ~= "" then
if e.keyCode == KeyCode.LEFT then self.iskeyleft = true end
+
animtimer = animtimer - e.deltaTime
if e.keyCode == KeyCode.RIGHT then self.iskeyright = true end
+
if animtimer <= 0 then
if e.keyCode == KeyCode.UP then self.iskeyup = true end
+
frame += 1
if e.keyCode == KeyCode.DOWN then self.iskeydown = true end
+
animtimer = animspeed
if e.keyCode == KeyCode.SPACE then self.iskeyspace = true end
+
if frame > #anims[currentanim] then
 +
frame = 1
 +
end
 +
bitmap:setTextureRegion(anims[currentanim][frame])
 +
end
 +
end
 
end
 
end
 +
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
 +
</syntaxhighlight>
  
function Sprite_Maker:onKeyUp(e)
+
==== In your game ====
-- keys released
+
Then all you need to do is create an instance of the player in your game class.
if e.keyCode == KeyCode.LEFT then self.iskeyleft = false end
+
<syntaxhighlight lang="lua">
if e.keyCode == KeyCode.RIGHT then self.iskeyright = false end
+
local myhero = Player.new()
if e.keyCode == KeyCode.UP then self.iskeyup = false end
+
</syntaxhighlight>
if e.keyCode == KeyCode.DOWN then self.iskeydown = false end
+
 
if e.keyCode == KeyCode.SPACE then self.iskeyspace = false end
+
=== Assets ===
end
+
You can pick the files from opengameart.org:
</source>
+
* https://opengameart.org/content/space-soldier-resize-64x64
 +
* https://opengameart.org/content/pixel-coins-asset
  
<!--
 
=== <translate>A YouTube Video</translate> ===
 
A step by step video on how to use this platformer template. Enjoy!
 
  
https://www.youtube.com/watch?v=WPcbsgOCnLU
+
{{GIDEROS IMPORTANT LINKS}}
-->
 

Latest revision as of 23:51, 18 November 2023

Here you will find various resources to help you create games and apps in Gideros Studio.

note: You may have to provide your own assets (fonts, gfx, …).
note 2: You can copy/paste the code :-)

Description

This section deals with animating any of your sprites (player, enemies, collectibles, ...). It can be used either in conjunction with cbump, liquidfun(box2d) or as standalone.

Spritesheet Animations (player, enemies, collectibles, npcs, ...)

This is an example of how to animate using a spritesheet.

If you are using a class

Player = Core.class(Sprite)

function Player:init()
	-- spritesheet animations
	local spritesheettex = Texture.new("gfx/playable/mr_man.png")
	local cols, rows = 8, 6
	self.spritesheetimgs = {} -- table that will hold all the images in the sprite sheet
	self.anims = {} -- table that will hold all our animations ("idle", "run", ...)
	self.currentanim = ""
	self.frame = 0
	self.animspeed = 1 / 7
	self.animtimer = self.animspeed
	-- 1 retrieve all anims in spritesheet
	local w, h = spritesheettex:getWidth() / cols, spritesheettex:getHeight() / rows
	local spritesheettexregion = nil
	for r = 1, rows do
		for c = 1, cols do
			spritesheettexregion = TextureRegion.new(spritesheettex, (c - 1) * w, (r - 1) * h, w, h)
			self.spritesheetimgs[#self.spritesheetimgs + 1] = spritesheettexregion
		end
	end
	-- 2 create animations
	self:createAnim("idle", 1, 4)
	self:createAnim("run", 9, 14)
	self.currentanim = "run"
	-- 3 we can now create the player
	self.bitmap = Bitmap.new(self.spritesheetimgs[1])
	self.bitmap:setAnchorPoint(0.5, 0.5)
	self.bitmap:setScale(2)
	self:addChild(self.bitmap)
	-- LISTENERS
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end

-- PLAYER LOOP
function Player:onEnterFrame(e)
	-- animations
	if self.currentanim ~= "" then
		self.animtimer = self.animtimer - e.deltaTime
		if self.animtimer <= 0 then
			self.frame += 1
			self.animtimer = self.animspeed
			if self.frame > #self.anims[self.currentanim] then
				self.frame = 1
			end
			self.bitmap:setTextureRegion(self.anims[self.currentanim][self.frame])
		end
	end
	self.bitmap:setPosition(128, 64)
end

-- FUNCTIONS
function Player:createAnim(xanimname, xstart, xfinish)
	self.anims[xanimname] = {}
	for i = xstart, xfinish do
		self.anims[xanimname][#self.anims[xanimname] + 1] = self.spritesheetimgs[i]
	end
end

If you are not using a class

-- spritesheet animations
local spritesheettex = Texture.new("gfx/hero/HQ_Trooper_all.png") -- change accordingly
local cols, rows = 6, 13 -- change accordingly
local spritesheetimgs = {} -- table that will hold all the images in the sprite sheet
local anims = {} -- table that will hold all our animations ("idle", "run", ...)
local currentanim = ""
local frame = 0
local animspeed = 1 / 7 -- change accordingly
local animtimer = animspeed

-- 1 retrieve all anims in spritesheet
local w, h = spritesheettex:getWidth() / cols, spritesheettex:getHeight() / rows
local spritesheettexregion = nil
for r = 1, rows do
	for c = 1, cols do
		spritesheettexregion = TextureRegion.new(spritesheettex, (c - 1) * w, (r - 1) * h, w, h)
		spritesheetimgs[#spritesheetimgs + 1] = spritesheettexregion
	end
end

-- 2 create animations
function createAnim(xanimname, xstart, xfinish)
	anims[xanimname] = {}
	for i = xstart, xfinish do
		anims[xanimname][#anims[xanimname] + 1] = spritesheetimgs[i]
	end
end
createAnim("idle", 1, 2)
createAnim("run", 25, 30)

-- 3 we can now create the player
local bitmap = Bitmap.new(spritesheetimgs[1])
bitmap:setAnchorPoint(0.5, 0.5)
bitmap:setPosition(128, 64)
stage:addChild(bitmap)
currentanim = "idle"
--currentanim = "run"

-- GAME LOOP
function onEnterFrame(e)
	if currentanim ~= "" then
		animtimer = animtimer - e.deltaTime
		if animtimer <= 0 then
			frame += 1
			animtimer = animspeed
			if frame > #anims[currentanim] then
				frame = 1
			end
			bitmap:setTextureRegion(anims[currentanim][frame])
		end
	end
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

In your game

Then all you need to do is create an instance of the player in your game class.

local myhero = Player.new()

Assets

You can pick the files from opengameart.org: