Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Basic Sprite Movement

Author
Message
bizzy401
20
Years of Service
User Offline
Joined: 23rd Oct 2005
Location:
Posted: 23rd Oct 2005 22:45
I find all of the tutorials to be rather overwhelming. I have gotten asa far as to get my sprite on the screen but as far as to make him move...lol

I was trying to amke a function that would control all of the main player movement. (Side scroller) any help or suggestions is GREATLY appreciated.

Its hard out here for a n00b...
NanoBrain
21
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 24th Oct 2005 02:45
bizzy401,

Notice, that to put a sprite onto the screen, it involves inputing the coordinates of where you wish that sprite to appear.



To make a sprite walk across the screen(left to right), you would want to edit its x position. Therefore:



This is as simple as the explaination gets in this topic. By the way, any source code you post will not make it through the holding cell until the mods trust you and free you from their standard newbie watching. so, post any code in code snippets(use the code button above the emoticons, when posting a reply.



+NanoBrain+
bizzy401
20
Years of Service
User Offline
Joined: 23rd Oct 2005
Location:
Posted: 24th Oct 2005 18:30
Thanks for the tip...I acctually figured it out by the time I was able to check the forums again, but I used a different method. Yours is much more simple. Now I need to move on to gravity and jumping...a little more complicated...time to search the forums!!!

Its hard out here for a n00b...
bizzy401
20
Years of Service
User Offline
Joined: 23rd Oct 2005
Location:
Posted: 25th Oct 2005 23:28
Okay I got movement down, but I am having problems with functions.
Why does this work

=CODE=


backdrop off
hide mouse
sync on
sync rate 120
set display mode 320,240,16
set image colorkey 255,0,255
load image "luigi.png",1
load image "luigi2.png",2
load music "mario.mid",1

play music 1

gavity as boolean

lx=25
ly=200
gravity = 1
i = 1
do
cls 0
sprite 1,lx,ly,i

if rightkey() = 1 and lx < 320
inc lx
i = 1
endif

if leftkey() = 1 and lx > 0
dec lx
i = 2
endif

sync
loop

=CODE=

But this doesn't...

=CODE=

backdrop off
hide mouse
sync on
sync rate 120
set display mode 320,240,16
set image colorkey 255,0,255
load image "luigi.png",1
load image "luigi2.png",2
load music "mario.mid",1

play music 1

gavity as boolean

lx=25
ly=200
gravity = 1
i = 1

do
cls 0
sprite 1,lx,ly,i

luigi()

sync
loop

function: luigi

if gravity = 1
ly = ly + 1
endif

if rightkey() = 1 and lx < 320
inc lx
i = 1
endif

if leftkey() = 1 and lx > 0
dec lx
i = 2
endif

endfunction

=CODE=

I have been reading on functions but I think I am just not getting it

Its hard out here for a n00b...
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 13th Nov 2005 02:06
Quote: "Okay I got movement down, but I am having problems with functions.
Why does this work"


Unfortunately this thread showed no new messages so your new messages... so this thread has probably been skipped by a lot of people.

Function Lesson:
The following won't be pretty... but here goes.

When you define things outside of the function like "lx=25" lx will be 25 outside of the function... but inside the function it will start at equaling zero. Any changes inside the function will work... but when it gets out of the function it will still be what lx equaled before entering the function.

For Pro it's easy to just define the variable as global so it can be used inside a function.



You can also have functions return information... you can have as many variables go into a function but can only have one variable return information.



In the above snip the function is called with "a=ChangeStuff(a,b)". The function itself is made with "function ChangeStuff(x,y)"... so inside the function it defines "x" as whatever "a" is and "y" as whatever "b" is. Inside the function "x" and "y" are changed... but because the end of the function has "endfunction x" it returns whatever "x" has become while going through the function. So the line that called the function "a=ChangeStuff(a,b)" is basically "a=x" (the "x" from inside the function).

End of Lesson

I personally would just put all the player stuff in an array for ease of use. Arrays are automatically defined as global.

User Defined Type:


A regular array:


Hope this helps.

Login to post a reply

Server time is: 2026-07-06 18:58:52
Your offset time is: 2026-07-06 18:58:52