Questions about platformer game

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.
gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Tue Sep 04, 2012 1:50 pm

while doing progress with the game coding i came up with another problem.

I have declared a function for drawing players in a standing position.
Currently i am trying to add a second player (npc) on the screen.


The problem is that the drawing function of the second player is using the same function with the main player just with different arguments. The result is that the main player's motion is totaly fucked up. When i stop calling the second player's drawing function then everything returns to normal.

For me with my limited knowledge in oop and love2d that is a bit weird and it should have worked. But i think i've found a method to solve this issue and that is by assigning a instance of the basic template standing function for every character created. It doesn't sound very economic though but this is the only solution i can think of, at least for now.

Any help is very appreciated ^^.

User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Questions about platformer game

Post by Roland_Yonaba » Tue Sep 04, 2012 4:17 pm

gregkwaste wrote: The problem is that the drawing function of the second player is using the same function with the main player just with different arguments. The result is that the main player's motion is totaly fucked up. When i stop calling the second player's drawing function then everything returns to normal.
Well, can you provide your .love file ? Or at least, post the part of your code which you expect to be wrong.

gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Tue Sep 04, 2012 5:18 pm

i can't post the entire love, it has become pretty big :P

but here is the lua for the class creation:

Code: Select all

require("AnAL")
require("moves")
--generic class
player={xcoord=200,ycoord=350}
npctable={}
--table for player types
types={"worm","kota","gria","bazelos","gauros"}



--RULES FOR SPRITES AND ANIMATIONS

--INDEX				ANIMATION
--1					STANDING
--2					INBETWEEN
--3					TURNING
--4					WALKING
--5					JUMP
--6					DUCK
--7					DUCKINV
--8					PUNCH
--9					ROLL
--10				RUN
--11				KICK
--12				HAKI



--create metatable for prototypes
player.__index=player



--create new player method
function player:new(type)
local self={}
setmetatable(self,player)

if type==types[1] then
self.sprites={
createsprite("chars/worm/wormstanding.png"),
createsprite("chars/worm/worminbetween.png"),
createsprite("chars/worm/wormturning.png"),
createsprite("chars/worm/worm.png"),
createsprite("chars/worm/wormjumpingnew.png"),
createsprite("chars/worm/wormducking.png"),
createsprite("chars/worm/worminvertducking.png"),
createsprite("chars/worm/wormpunch01.png"),
createsprite("chars/worm/wormrolling.png"),
createsprite("chars/worm/wormrunning.png"),
createsprite("chars/worm/wormkick01.png"),
createsprite("chars/worm/wormjumphaki.png"),
createsprite("chars/worm/wormcrawling.png"),
createsprite("chars/worm/wormvanish.png")
}

self.animations={
newAnimation(self.sprites[1],64,64,0.09,8),
newAnimation(self.sprites[2],64,64,0.5,3),
newAnimation(self.sprites[3],64,64,0.5,5),
newAnimation(self.sprites[4],64,64,0.5,12),
newAnimation(self.sprites[5],64,64,1,15),
newAnimation(self.sprites[6],64,64,0.5,5),
newAnimation(self.sprites[7],64,64,0.5,5),
newAnimation(self.sprites[8],100,100,0.5,7),
newAnimation(self.sprites[9],64,64,0.5,13),
newAnimation(self.sprites[10],64,64,0.5,4),
newAnimation(self.sprites[11],64,64,0.5,10),
newAnimation(self.sprites[12],64,64,0.5,23),
newAnimation(self.sprites[13],64,64,0.5,5),
newAnimation(self.sprites[14],64,64,0.5,8)
}




self.mass=10
self.yvel=10
self.speed=7

function self:draw()
love.graphics.print("drawingfunction",750,200)
end


elseif type==types[2] then

self.sprites={
createsprite("chars/kota/kotastanding.png")
}
self.animations={
newAnimation(self.sprites[1],64,64,0.5,9)
}

function self:draw()
kota_draw(self)
end


end

self.states={
freefall=1,
face=1,
walking=0,
standing=0,
turning=0,
duck=0,
jump=0,
roll=0,
attack=0,
landing=0,
running=0,
haki=0,
stall=0,
lose=0,
death=0,
crawl=0
}






return self
end

function npc_create(name,type)

name=player:new(type)
table.insert(npctable,name)



end



function createsprite(path)
return love.graphics.newImage(path)
end


function kota_draw(name)

stand(name,name.facestate)

end


here is the stand function alone:

Code: Select all

function stand(name,orientation)
inbetweenstatus=0
duckstatus=0
turningstatus=0
name.standingstate=1
name.inbetweenstate=0
name.turningstate=0
name.walkingstate=0
name.states.haki=0
afunc="stand"

if orientation==1 then
name.animations[1]:draw(name.xcoord,name.ycoord,0,orientation,1)
else
name.animations[1]:draw(name.xcoord+64,name.ycoord,0,orientation,1)
end
love.graphics.print("STANDING",300,30)



for i=2,14,1 do

if name.animations[i] then
name.animations[i]:reset()
end
end
end

and it is called simultaenously
for the player character as a stand(worm,worm.states.face)

and after initializing an npc character npc1=player:new("kota")
with npc1:draw() which ends to stand(npc1,npc1.states.face)


stand function seems that it cannot handle two simultaenous calls....


Edit: Now that i am thinking it clearer, the function is indeed called simultaenously for both characters... i think i need to find a way to call it once for every character:

Enter all my characters in a table and iterate the table to execute the standing function for every character on the scene, one by one...
Am I correct or am i missing somethin?


Edit2: I solved it with the second way, i also needed to correct some variable names on the stand function ^^.
Everything back on smoooooooooooooooooooth again :cool: :cool: :cool:

gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Wed Sep 05, 2012 9:11 am

Ok, here i am again with another question :P (i hope i solve it alone this time too, but now it seems thats beyond my power...)

As i said a few posts ago, i managed to make a moving platform:

At first i made an object layer in tiled, add inside some tile objects in line in order to make the platform shape and then i assigned to the first object a property called "index".

Afterwards i created a function that moves every object in that specific object layer.

So far so good.

Now i am trying to add collision detection to the platform. For the rest of my map i am checking if there is a tile with a specific property in my players position and then i snap my player's position to the tile's calculated y position.

The problem is that the data organization in objectlayers is not the same with the tilelayers (advanced tile loader), so there is no table that stores the map coordinates of the object in order to keep checking if my player's coordinates are inside the object tile...

it seems like i'll necessarily use the hardoncollider for that :/

gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Mon Sep 10, 2012 1:33 pm

Ok, some new lua programming questions:

is it possible from a table value somehow to get the whole table?

the purpose of this is the following:

i have a general table with all the players on the scene
every index of this table is a table as well with all the individual stats and function that every character needs. One of these stats is a hardoncollider rectangle.

So whenever hardoncollider detects a collision shape_one or shape_two is a player.box value. So i can't find a way of getting from shape_one or shape_two ,back to the player table in order to change some other values for exampe the player.health value.

Is there any work around for that?

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Questions about platformer game

Post by Robin » Mon Sep 10, 2012 2:04 pm

gregkwaste wrote:is it possible from a table value somehow to get the whole table?
Not unless you make a link yourself.

If player.box is a table, you can do something like:

Code: Select all

player.box.player = player
Then you can do something like:

Code: Select all

local player = shape_one.player
Help us help you: attach a .love.

gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Mon Sep 10, 2012 2:23 pm

Robin wrote:
gregkwaste wrote:is it possible from a table value somehow to get the whole table?
Not unless you make a link yourself.

If player.box is a table, you can do something like:

Code: Select all

player.box.player = player
Then you can do something like:

Code: Select all

local player = shape_one.player

Thanks for your answer.
That is a possible solution i was thinking of too, but it does not sound economic :/

i mean player is a pretty big table, in this case i will actually double its size, unless of course lua stores pointers when needed.

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Questions about platformer game

Post by Nixola » Mon Sep 10, 2012 2:25 pm

That uses a pointer (I think, anyway the table isn't duplicated); player and player.box.player are EXACTLY the same, if you modify one of them the other one gets modified as well
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Mon Sep 10, 2012 2:28 pm

Nixola wrote:That uses a pointer (I think, anyway the table isn't duplicated); player and player.box.player are EXACTLY the same, if you modify one of them the other one gets modified as well

Just great ^^. I'll apply it right away :)

gregkwaste
Prole
Posts: 19
Joined: Fri Aug 31, 2012 8:32 am

Re: Questions about platformer game

Post by gregkwaste » Mon Sep 10, 2012 2:43 pm

Well there is another problem.... player.box can't be indexed...

When player.box=collider:addRectangle(etctectectect), player.box.player=player can't be assigned because player.box is not a table :/

Post Reply

Who is online

Users browsing this forum: MrFariator and 9 guests