Problem with Changing Sprite of a 2D Actor

Posted By: Espér

Problem with Changing Sprite of a 2D Actor - 03/15/08 17:06

ok.. this is a great edit of my mini Script ^^
i edited it, so i only have 1 Function, but different handles to call the directions and sprites.

here´s the code:
Code:

//////////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
//////////////////////////////////////////////////////////////////////////
//
// VARIABLEN DEFINIEREN
//
//////////////////////////////////////////////////////////////////////////

var sprite_frames = 6; // Spriteanzahl fur Standani
var switcher = 0; // Sheetswitcher
var heldenblickrichtung = 6; // Der Name sagts schon

var counter = 0; // Startframe
var bewegungsmuster = 0; // Wie bewegt sich der Held


var tester = 0; // Nur für Debug Zwecke

BMAP* alucard_sheet = "Alucard_stand_r.tga";
STRING* debugger1 = bewegungsmuster;

//////////////////////////////////////////////////////////////////////////
//
// DIE WINDOWS
//
//////////////////////////////////////////////////////////////////////////

PANEL* hero =
{
window (320,240,24,48,alucard_sheet,counter,0);
flags = VISIBLE;
}

PANEL* shower =
{
window (0,500,800,64,alucard_sheet,0,0);
digits ( 10,10,10,*,1,counter);
digits ( 10,25,10,*,1,hero.size_x);
digits ( 60,25,10,*,1,sprite_frames);
digits ( 110,25,10,*,1,tester);
flags = VISIBLE;
}

//////////////////////////////////////////////////////////////////////////
//
// DIE BEWEGUNGSCODES
//
//////////////////////////////////////////////////////////////////////////


function move(bewegungsmuster)
{



if (bewegungsmuster == 6)
{
if (heldenblickrichtung != 6)
{
alucard_sheet = "Alucard_walk_r.tga";
hero.size_x = 40;
sprite_frames = 16;
counter = 0;
switcher = 0;
heldenblickrichtung = 6;
}
hero.pos_x += 3;
}

if (bewegungsmuster == 4)
{
if (heldenblickrichtung != 4)
{
alucard_sheet = "Alucard_walk_l.tga";
hero.size_x = 40;
sprite_frames = 16;
counter = 0;
switcher = 0;
heldenblickrichtung = 4;
}
hero.pos_x -= 3;
}

if (bewegungsmuster == 0)
{
if (heldenblickrichtung == 4 && switcher != 1)
{
alucard_sheet = "Alucard_stand_l.tga";
hero.size_x = 24;
sprite_frames = 6;
counter = 0;
switcher = 1;
}
if (heldenblickrichtung == 6 && switcher != 1)
{
alucard_sheet = "Alucard_stand_r.tga";
hero.size_x = 24;
sprite_frames = 6;
counter = 0;
switcher = 1;
}
}



}



//////////////////////////////////////////////////////////////////////////
//
// DIE SPRITE und KEY FUNKTION
//
//////////////////////////////////////////////////////////////////////////
function move_hero()
{
while(1)
{
if (key_d == 1) {move(6);}
if (key_a == 1) {move(4);}
if (key_a == 0 && key_d == 0) {move(0);}


tester = hero.size_x * sprite_frames;
wait(1);
}
}

function move_sprite()
{
while(1)
{
if (counter >= ((hero.size_x * sprite_frames) - hero.size_x))
{
counter = 0;
}
else
{
counter += hero.size_x;
}
wait(10);
}
}


//////////////////////////////////////////////////////////////////////////
//
// DIE MAIN FUNKTION
//
//////////////////////////////////////////////////////////////////////////

function main()
{
fps_max = 200;
video_mode = 7;
screen_color.blue = 150;
wait(-1);
move_hero();
move_sprite();
}



My problem is..
When i start the script, the actor moves by itself to a direction.. but i hit no key...
What should he do?
1.) He must play the stand animation when no key is hitten
2.) he must play the walking animations when a direction key is pressed
3.) The size of the Window must be variable, because the stand sprite has a Framesize of 24 pixels... and the walk animations of 40 pixels...

In this Zip File are the Prite_Lines i used in that code:

Hope someone knows an answer to this... <a href="/Sonstiges/players_movement.zip" target="_blank">>>Click me<<</a>
IMPORTANT!!!: The Script in that Zip Acrhive is an older version.. so don´t loom at them ^^.. just at the Sprites if you want to try some thing with them.
Posted By: Widi

Re: Error with a function call - 03/15/08 17:16

move_right; == false
move_right(); == ok
Posted By: dblade

Re: Error with a function call - 03/15/08 17:18

I think you forgot the parameters, when you call the functions, it should look like this:

Code:

move_right();
move_left();
move_stand();


Posted By: xbox

Re: Error with a function call - 03/15/08 17:20

Yeah, move_right; is wrong.
It needs to be:
move_right();
Posted By: Vadim647

Re: Error with a function call - 03/15/08 17:26

Problems:
1. my_function(); not my_function;
2. Maybe use {} brackets...
3. Move_left() moved character right.
So, entire corrected code:
Code:

//////////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
//////////////////////////////////////////////////////////////////////////

var sprite_stand_frames = 6; // Spriteanzahl fur Standani

var counter = 0; // Startframe

//////////////////////////////////////////////////////////////////////////

PANEL* hero =
{
//bmap = "Alucard_stand.tga";
window (320,240,24,48,"Alucard_stand.tga",counter,0);
flags = VISIBLE;
}

PANEL* wertausgabe =
{
digits ( 10,10,10,*,1,counter);
flags = VISIBLE;
}

//////////////////////////////////////////////////////////////////////////

function move_right()
{
hero.pos_x += 2;
wait(1);
}

function move_left()
{
hero.pos_x -= 2;
wait(1);
}

function move_stand()
{
while(1)
{
if (counter >= (24 * sprite_stand_frames))
{
counter = 0;
}
else
{
counter += 24;
}

wait(60);
}
}

//////////////////////////////////////////////////////////////////////////

function main()
{
fps_max = 60;
video_mode = 7;
screen_color.blue = 150;
wait(-1);

while(1)
{
if (key_d == 1) {move_right();}
if (key_a == 1) {move_left();}
if (key_a == 0 && key_d == 0) {move_stand();}
wait(1);
}
}


Posted By: dblade

Re: Error with a function call - 03/15/08 17:37

Code:

while(1)
{
if (key_d == 1) {move_right();}
if (key_a == 1) {move_left();}
if (key_a == 0 && key_d == 0) {move_stand();}
wait(1);
}



I think this is a possible way but in LiteC it's not necessary that you use brackets when you only call one function so this is also alowed:

Code:

while(1)
{
if (key_d == 1)
move_right();
if (key_a == 1)
move_left();
if (key_a == 0 && key_d == 0)
move_stand();
wait(1);
}


Posted By: Vadim647

Re: Error with a function call - 03/15/08 17:46

'{}' In that i'm wrong.
+1 I'm not sure, but move_stand() causes unit animation speed-up. If it's not planned, delete 'while (1) {' and 'wait(60)'. It will cause 1 pic frame per frame animation.
+2 If you want 1 frame\sec move 'move_stand' before 'while' loop in main.
Posted By: Espér

Re: Error with a function call - 03/15/08 17:54

ok.. if i use the code from Vadim647, the stand animation works to fast...
how to correct that?
Posted By: Lukas

Re: Error with a function call - 03/15/08 18:31

You could chose a higher value at wait(60);

Posted By: Espér

RE: Problem with showing a 2D Actor correctly - 03/16/08 11:44

Quote:

!!!!! Look at the Start Post !!!!!



Posted By: Widi

Re: RE: Problem with showing a 2D Actor correctly - 03/16/08 12:25

Extrem schwierig, den Code zu lesen, wenn die Klammern nicht richtig eingeschoben sind. So wie ich es verstehe, wird in der main while(1) Schleife
geschaut, ob key_a und key_d = 0 (NICHT GEDRüCKT) sind, worauf move(0)ausgeführt wird.
In der Function move wird der code ab "if (heldenblickrichtung == 6)" ausgeführt, und da steht "hero.pos_x -= 2;", womit der Player bewegt wird.
Warum hast du in der Function move() nochmals while(1) Schleifen? Ein mal eine Taste gedrückt und dein Hero bewegt sich immer weiter, auch wenn die Taste losgelassen wird.
Hoffe das hilft, Gruss aus der verschneiten Schweiz...
Posted By: Espér

Re: Problem with showing a 2D Actor correctly - 03/16/08 14:19

Ich hab das Script im Startpost ma umeditiert.. jetzt bewegt sichd er Chara schneller... aber die Sprite wechselt er trotzdem nicht...

nunja.. die while(1) in den einzelnen Bewegungen, ist für die Animation des Sprite-Line-Sheets...

Der Player hat in dem Script 3 bewegungen.. weshalb im move(XXX) ja auch Statt dem XXX eine zahl steht..
Diese zahl steht für die Richtungen auf dem Ziffernblock der Tastatur.. also auch der Bewegungsrichtung ( 0 = stehen ).
Ich frage in der Function ab, ob bewegungsmuster ( der wert aus den Klammern ) == 4,6 oder 0 ist.. Je nachdem wird ein andrer Code ausgeführt..
Posted By: Espér

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 18:07

I can´t edit the first post.... WHY????

So i must do it here.. i reedited the code, so i´ve two Scripts..
here´s the Main Code:
Code:

//////////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
#include "players_movement.c"
//////////////////////////////////////////////////////////////////////////
//
// DIE MAIN FUNKTION
//
//////////////////////////////////////////////////////////////////////////

function main()
{
fps_max = 60;
video_mode = 7;
screen_color.blue = 150;
wait(-1);
move_hero(); // Starting Key If´s and movement
move_sprite(); // Starting Sprite animation
}




AND HERE, the Code for Player Movement:
Code:

//////////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
//////////////////////////////////////////////////////////////////////////
//
// VARIABLEN DEFINIEREN
//
//////////////////////////////////////////////////////////////////////////

var sprite_frames = 6; // Spriteanzahl für Standani
var switcher = 0; // Sheetswitcher
var heldenblickrichtung = 6; // Der Name sagts schon

var counter = 0; // Startframe
var bewegungstempo = 1; // Wie schnell bewegt sich der Held


var tester = 0; // Nur für Debug Zwecke


//////////////////////////////////////////////////////////////////////////
//
// DIE WINDOWS
//
//////////////////////////////////////////////////////////////////////////

PANEL* hero =
{
window (320,240,24,48,"Alucard_stand_r.tga",counter,0);
flags = VISIBLE;
}

PANEL* shower =
{
digits ( 10,10,10,*,1,counter);
digits ( 10,25,10,*,1,hero.size_x);
digits ( 60,25,10,*,1,sprite_frames);
digits ( 110,25,10,*,1,bewegungstempo);
flags = VISIBLE;
}

//////////////////////////////////////////////////////////////////////////
//
// DIE BEWEGUNGSCODES
//
//////////////////////////////////////////////////////////////////////////


function move_hero()
{
while(1)
{
if (key_d == 1)
{
if (heldenblickrichtung != 6)
{
hero = bmap_create("Alucard_walk_r.tga");
hero.size_x = 40;
sprite_frames = 16;
counter = 0;
switcher = 0;
heldenblickrichtung = 6;
}
// hero.pos_x += bewegungstempo;
camera.x += bewegungstempo;
}

if (key_a == 1)
{
if (heldenblickrichtung != 4)
{
hero = bmap_create("Alucard_walk_l.tga");
hero.size_x = 40;
sprite_frames = 16;
counter = 0;
switcher = 0;
heldenblickrichtung = 4;
}
// hero.pos_x -= bewegungstempo;
camera.x -= bewegungstempo;
}

if (key_d == 0 && key_a == 0)
{
if (heldenblickrichtung == 4 && switcher == 0)
{
hero = bmap_create("Alucard_stand_l.tga");
hero.size_x = 24;
sprite_frames = 6;
counter = 0;
switcher = 1;
}
if (heldenblickrichtung == 6 && switcher == 0)
{
hero = bmap_create("Alucard_stand_r.tga");
hero.size_x = 24;
sprite_frames = 6;
counter = 0;
switcher = 1;
}
}
wait(1);
}
}



//////////////////////////////////////////////////////////////////////////
//
// DIE SPRITE und KEY FUNKTION
//
//////////////////////////////////////////////////////////////////////////
function move_sprite()
{
while(1)
{
if (counter >= ((hero.size_x * sprite_frames) - hero.size_x))
{
counter = 0;
}
else
{
counter += hero.size_x;
}
wait(10);
}
}




Same problem.. He doesn´t change the Sprite.... HELP


Ps.: with
Code:
hero.bmap = "Filename"


The Acknet.exe crashes... The game shuts down via Windows Error...
Posted By: Uhrwerk

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 18:55

Code:

while(1)
{
PANEL* hero =
{
window(320,240,24,48,alucard_sheet,counter,0); flags = VISIBLE;
}
}


A panel definition inside a while loop that runs forever and is not included in a function?? Hell, how did you get the compiler to take that. That's total nonsense.
Code:
BMAP* alucard_sheet = "Alucard_stand_r.tga";


You're assigning an array of characters to a bitmap. You can't do that. If you want to set a bitmap you have to assign a bitmap, e.g. with bmap_create(). You have done this several times in you code. Always the same error.
Posted By: Espér

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 19:12

ok... the while(1) loop is deleted.. but how to use the bmap_create() code???

Can you give me an example, please?


Have updated the code above...
Posted By: Uhrwerk

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 20:08

Once again you confused the types of your variables. You assigned a bitmap pointer to a panel pointer. An valid example would be
Code:
hero->bmap = bmp_create("Alucard_stand_r.tga");


If you do this continuously you will create a memory leak as you continue to create new bitmaps. A better solution would be loading bitmaps at the beginning of your code and assigning them later. E.g.:
Code:
BMAP* alu_stand_r;
BMAP* alu_stand_l;
...
void move_hero()
{
alu_stand_r = bmap_create("Whatever filename");
alu_stand_l = bmap_create( // and so on.
// Now imagine here all your loops and if statements and there:
hero->bmap = alu_stand_r;
}


If you're assigning variables in your code then always ensure they have the same type. If the type is different your code won't get compiled or you'll gain unwanted results including crashes.

Be sure to understand the concept of variables and types. Read the manual und lite-c -> variables.
Posted By: Espér

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 20:14

ok..but i can´t just load Bitmaps.. i must animnate them.. so they are Sprite-Lines...
I must create a panel with a window ( see the code ).. and the bitmap of that window must be Changed.. BUT HOW...
Posted By: Uhrwerk

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 20:56

If you want to use the window then place all animation frames in one bitmap and use varX and varY of the window to animate it.
Posted By: Espér

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 21:03

Yes.. but.. my Problem is, if i create the Bitmaps like you said:
Code:

...
BMAP* alu_stand_r;
BMAP* alu_stand_l;
BMAP* alu_walk_r;
BMAP* alu_walk_l;


//////////////////////////////////////////////////////////////////////////
//
// DIE WINDOWS
//
//////////////////////////////////////////////////////////////////////////

PANEL* hero =
{
window (320,240,24,48,"Alucard_stand_r.tga",counter,0);
flags = VISIBLE;
}

PANEL* shower =
{
digits ( 10,10,10,*,1,counter);
digits ( 10,25,10,*,1,hero.size_x);
digits ( 60,25,10,*,1,sprite_frames);
digits ( 110,25,10,*,1,bewegungstempo);
flags = VISIBLE;
}

//////////////////////////////////////////////////////////////////////////
//
// DIE BEWEGUNGSCODES
//
//////////////////////////////////////////////////////////////////////////


function move_hero()
{
alu_stand_r = bmap_create("Alucard_stand_r.tga");
alu_stand_l = bmap_create("Alucard_stand_l.tga");
alu_walk_r = bmap_create("Alucard_walk_r.tga");
alu_walk_l = bmap_create("Alucard_walk_l.tga");

while(1)
{
...



The Bitmaps are shown in the upper left corner.. here´s a screen:


i want the hero Sprite ( the centered one ) to be changed...
Posted By: Uhrwerk

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 22:32

If you want to change an entities appearance you should either use the frame parameter and create a bitmap in the form "mybitmap+9.bmp" or use the ent_morph() instruction.
Posted By: Espér

Re: Problem with Changing Sprite of a 2D Actor - 03/19/08 23:24

YEAH::IT WORKS..THANKS ALOT.. *plz close this Thread*
© 2024 lite-C Forums