Вы находитесь на странице: 1из 3

Advanced gamemaker tutorials

1: Swinging rope realistically

For this tutorial, you'll need an object called “sp”, which is the point at which you are swinging
(SP: swing point). You can do this by making a grapple object that flies and creates an SP when it
lands.
All the changes are made to the main character in the game.
For his create event, initialize a variable “dis”
secondly, you'll initialize a variable “wtconst”, the wieght constant. The higher this variable is,
the heavier the player is. I use .4
Thirdly, you'll initialize a variable “fricconst”, the friction constant. The higher this is, the less
the player swings. Mine is 8.
further changes are made to the step event.
Input this code:
if instance_exists(sp)
if dis=0
{spd=0
dis=point_distance(x,y,sp.x,sp.y)
dir=0
d2=0
spx=sp.x
spy=sp.y
pspx=spx
pspy=spy
}
now for the next code:

if instance_exists(sp)
{
//first it figgures out what direction it needs to swing based on where the swing point is.
dirrot=sign(x-spx)
//next it figgures out what the speed of the swing is, based on the weight and the friction.
spd-=(dirrot*wtconst)+(sign(spd)/fricconst)
//finally, it calculates the new position based on the past position and the current one.
dir=point_direction(x+lengthdir_x(spd,dir-90),y+lengthdir_y(spd,dir-90),spx,spy)+180
//now it tests to see if it hits a solid object (block) and if it doesn't, it swings.
if not place_meeting(lengthdir_x(dis,dir)+spx,lengthdir_y(dis,dir)+spy,block)
{
x=lengthdir_x(dis,dir)+spx
y=lengthdir_y(dis,dir)+spy
}
}

now, do you want MORE advanced? How about using the “sticky rope” policy?
With this technique, when the rope hits “block” it sticks to it. IE when there is a block in
between the player and the swing point, he swings on the block. It's hard to explain. Imagine you were
swinging a rope and you put a dowel in the way. The rope would swing around that, right? Thats what
happens here.
Put this script in the step event after the first two.
if instance_exists(grapnel)
{//this is all random junk the loop requires.
xx=x
yy=y
// this is the target destination.
destx=spx
desty=spy
len=0
dir=point_direction(x,y,spx,spy)
do
{
// starts scanning down the rope.
xx+=lengthdir_x(1,dir)
yy+=lengthdir_y(1,dir)
// the ever present limiter. All of your loops should have one.
len+=1
// testing if there is a block in the way.
loc=0
loc=instance_position(xx,yy,block)
if not loc=0
{// recalculating the swing point
pspx=spx
pspy=spy
spx=xx
spy=yy
dis=point_distance(xx,yy,x,y)
}
}
until len>dis}

there is even another step, called the “loose rope” policy, but I haven't developed that yet. With
sticky rope, it sticks to any swing points. IE when you remove the dowel, it should return to the
standard swing. But instead, it “sticks” to the swing point.

Login?

Many people ask me how to do logins and user information.


The way THEY describe it is with logging onto your account, using the World Wide Web.
However, gamemaker doesn't do this very well. You would have to set up a server of some kind to store
all the players information, which
-leads to problems associated with servers, like keeping the program running, and using
different coding for what MUST be the same game.
-is insecure and risky.
-probably costs a lot of money.

However, the way I do it is through INI files. The player carries around a file, stored with the
game file, which contains his data and password. This way, you can store data relatively safely, using
the much easier-to-use INI functions.
To begin, create a blank text file in the same folder as the game. Call it “main.ini”
next you will require an object that will store the player's data once it is retrieved. Call this
object “DS” for data storage. You will also be able to read data from the ini while in play.
On DS's create event, put this code:

name=get_string('Name:','player')
pass=get_string('password:','')
//testing to see if the file exists, and creating it if it doesn't. Now you see what main.ini was for!
if !file_exists(name+'.ini')
{file_duplicate('main.ini',name+'.ini')
new=1}
else new=0
ini_open(name+'.ini')
// I think its obvious what's happening here!
if new=0
if not ini_read_string('data','pass','')=pass
{show_message('incorrect password')
game_restart()}
//creating the password data set
if new=1
ini_write_string('data','pass',pass)

tada! Now you're ready!

Whenever you want to READ data from the file, just do this:
data_you_want=ini_read_real('data','what_you_want',0)
whenever you want to WRITE progress to the file, just do this:
ini_write_real('data','what_to_write',data_you_want)

Вам также может понравиться