Flip/Mirror in AnAL !

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
NowakFul
Prole
Posts: 24
Joined: Mon Jul 28, 2014 7:13 pm

Flip/Mirror in AnAL !

Post by NowakFul »

Hello ! My name is Alexandre I'm a new French developper !
And today I'm here because I have one problems ..

I really, really search for long hours and I don't know how to flip the sprite in AnAL when the player going to the right !

Image

Can somebody help me please ?

This is the code !

Code: Select all

	require("AnAL")
	stop = love.graphics.newImage("image/character.png")
	stop:setFilter("nearest","nearest")
	stop = newAnimation(stop, 21, 30, 0.25, 1)
	stop:setMode("loop")
	walk = love.graphics.newImage("image/character.png")
	walk:setFilter("nearest","nearest")
	walk = newAnimation(walk, 21, 30, 0.07, 10 )
	walk:setMode("loop")
	jump = love.graphics.newImage("image/jump.png")
	jump:setFilter("nearest","nearest")
	jump = newAnimation(jump, 14, 27, 0.25, 2)
	jump:setMode("loop")

	
player = {}
function player.load()
	player.width = 180
	player.height = 50
	player.x = 900
	player.y = 836
	player.xvel = 0
	player.yvel = 0
	player.friction = 9.5
	player.speed = 1500
	player.ySpeed = 0
	player.gravSecond = 0.5
end

function player.physics(dt)
	player.x = player.x + player.xvel * dt
	player.y = player.y + player.yvel * dt
	player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
	player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))

end
--
function player.boundary()
	if player.x < 0 then
		player.x = 0
		player.xvel = 0
	end
	if player.y < 0 then
		player.y = 0
		player.yvel = 0
	end
	if player.x + player.width > screenWidth then
		player.x = screenWidth - player.width
		player.xvel = 0
	end
	if player.y + player.height > screenHeight then
		player.y = screenHeight - player.height
		player.yvel = 0
	end
end

function player.move(dt)
	if love.keyboard.isDown('d') and player.xvel < player.speed then
		player.xvel = player.xvel + player.speed * dt
	end
	if love.keyboard.isDown('q') and player.xvel > -player.speed then
		player.xvel = player.xvel - player.speed * dt
	end
	if love.keyboard.isDown('q') then
	walking = true
	end
	if love.keyboard.isDown('d') then
	walking = true
	end
	
end


function player.draw()

	  if(not walking)then stop:draw(player.x+5, player.y+33, 0, 1) else walk:draw(player.x+3, player.y+32, 0, 1) end
end
	
function love.keypressed(key)
   if key == "up" and player.y < 837 and not player.inAir then 
      player.ySpeed = -10
      player.inAir = true
   end
end
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: Flip/Mirror in AnAL !

Post by verilog »

Hi NowakFul, check out this post where I suggest a way of flipping a sprite with AnAL:

viewtopic.php?f=4&t=9825&p=60239#p60239
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: Flip/Mirror in AnAL !

Post by pielago »

this is how I do my sprites with AnAL
Attachments
stand.love
(24.75 KiB) Downloaded 138 times
NowakFul
Prole
Posts: 24
Joined: Mon Jul 28, 2014 7:13 pm

Re: Flip/Mirror in AnAL !

Post by NowakFul »

So.. Thanks for you answer !

But.. I use the technique of pielago and I got this error !

Image

This is the code in MAIN

Code: Select all

local loader = require ("Advanced-Tiled-Loader-master/Loader")
loader.path = "maps/"
local tWidth = 16
local tHeight = 16
local k=love.keyboard.isDown
require("camera")
require("conf")
require("menu")
require("menu2")
require ("ennemy")
require ("player")


function love.load()


	gamestate = "menu"
	player.load()

	if gamestate == "mapchoice" then
	a:load()
	end

	
	if gamestate == "menu" then
	p:load()
	end
	
			world =		{
				gravity = 1800,
				ground = 10,
				gravSecond = 1.5
				}

	
	love.graphics.setBackgroundColor(91, 133, 255)
	
	require("AnAL")
	love.window.setFullscreen(true, "desktop")
	map = loader.load("map1.tmx")
end


function love.draw()
	if gamestate == "mapchoice" then
	a:draw()
	end

	if gamestate == "menu" then
	p:draw()
	end


	if gamestate == "playing" then
	
	
	camera:set()
	
	player.draw()
	
	map:draw()
	end
end

function love.update(dt) 


	if gamestate == "menu" then
	p:update(dt)
	end
	
	if gamestate == "mapchoice" then
	a:update(dt)
	end

if gamestate == "playing" then

	player.physics(dt)

	camera:setPosition( player.x - (love.graphics.getWidth()/13), player.y - (love.graphics.getHeight()/15))
	
	
	walking = false;

	
	
	
	   frame = dt * 30 
   player.ySpeed = player.ySpeed + player.gravSecond * frame 
   player.y = player.y + player.ySpeed * frame 
   if player.y > 833 then 
      player.y = player.y - player.ySpeed * frame 
      player.ySpeed = 0 
      player.inAir = false

      end
	end
end


function love.keypressed(key)
   if key == "z" and player.y < 833 and not player.inAir then 
      player.ySpeed = -6 
      player.inAir = true
   end
end

function love.mousepressed(x, y, button)	
	if gamestate == "menu" then
	p:mousepressed(x, y, button)
	end
	if gamestate == "mapchoice" then
	a:mousepressed(x, y, button)
	end
end

And this is the code in PLAYER.LUA

Code: Select all

require("AnAL")
local g=love.graphics
local k=love.keyboard.isDown
local color={255,255,255,255}

player = {}
function player.load()
	player.width = 180
	player.height = 50
	player.x = 900
	player.y = 832
	player.xvel = 0
	player.yvel = 0
	player.friction = 9.5
	player.speed = 1500
	player.ySpeed = 0
	player.gravSecond = 0.5
	player.direction = "stand"
	--
	--
	walk_left   = g.newImage('image/walk_left.png')		
	walk_right  = g.newImage('image/walk_right.png')			

	wl =newAnimation(walk_left,40,40,0.1,10)
	dir ="left"	
	wr =newAnimation(walk_right,40,40,0.1,10)
	dir ="right"
	
	--stand cycle
	stand_left  = g.newImage('image/stand_left.png')		
	stand_right  = g.newImage('image/stand_right.png')			

	
	sl =newAnimation(stand_left,40,40,0.1,1)
	direction ="left"	
	sr =newAnimation(stand_right,40,40,0.1,0)
	direction ="right"

end

function player.physics(dt)
	player.x = player.x + player.xvel * dt
	player.y = player.y + player.yvel * dt
	player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
	player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))

end
--
function player.update()
	if k("q") then wl:update(dt); dir="left"   end
	if k("d") then wr:update(dt); dir="right"  end

	
	--stand cycle
	if direction =="left"   and  player.direction=="stand" then sl:update(dt) end
	if direction =="right"  and  player.direction=="stand" then sr:update(dt) end
	
	if k("d") and player.xvel < player.speed  then player.direction="right"; player.xvel = player.xvel + player.speed * dt end
	if k("q") and player.xvel > -player.speed then player.direction="left" ; player.xvel = player.xvel - player.speed * dt end
	

end




function player:keyreleased( key, unicode )

	if key=="q" then wl =newAnimation(wl,32,32,0.1,0);   dir ="left"   end
	if key=="d" then wr =newAnimation(wr,32,32,0.1,0);  dir ="right"  end
	
	if key=="q"and player.direction== "left"  then sl =newAnimation(stand_left,32,32,0.1,0);   direction ="left"   end
	if key=="d"and player.direction== "right" then sr =newAnimation(stand_right,32,32,0.1,0);  direction ="right"  end
	
	if (key == "d") or (key == "q") then  
    player.direction="stand"		
end
end

function player:draw()
	g.setColor(color)	
	if dir=="left"  then wl:draw(player.x ,player.y) end
	if dir=="right" then wr:draw(player.x ,player.y) end
	
	if direction =="left"   and  player.direction=="stand" then sl:draw(player.x ,player.y) end
	if direction =="right"  and  player.direction=="stand" then sr:draw(player.x ,player.y) end

end
Thanks you ! :)
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Flip/Mirror in AnAL !

Post by MadByte »

To flip your animation just use an negative sx (scale x) value as argument:

Code: Select all

player = {}
player.x = 100
player.y = 100
player.width = 32 -- frame width
player.height = 64 -- frame height
player.anim = newAnimation(yourImage, player.width, player.height, speed, frames)
player.direction = "left" -- I assume your sprite is normally facing left

function love.update(dt)
  yourAnim:update(dt)
end

function love.draw()

  -- anim:draw(x, y, rotation, scalex, scaley, offsetx, offsety) // These are the arguments (used when facing right)
  if player.direction == "left" then 
    player.anim:draw(player.x, player.y)
  elseif player.direction == "right" then
    player.anim:draw(player.x, player.y, 0, -1, 1, player.width) -- you need to set the offset to player.width to correct the position on screen accordingly
  end

end

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 197 guests