STEP 1 Enemy Damages You
Battle system when hit by Enemy
Now that you can attack enemies we also want the enemies to be able to attack you. The nice thing about scripts is that you can reuse them and we will do just that.
You will need to add this to all your monster step events.
This will replace your Enemy Collision area you may notice a new knock back code which does the same thing as before but is slightly more efficient.
The only new line of code is this:
oPlayer.hpamount-=AttackDamage(attack,global.armor)
this line of code will compare the attack of the monster to your armor and do damage accordingly.
Step event of all your enemies
//Enemy Collision from above
if (place_meeting(x,y,oPlayer))
{
if (oPlayer.y < y-sprite_height/2)
{
with (oPlayer)
vsp = -global.jumpspeed;
hpamount-=1; // This is the damage you do to a monster by jumping on his head
}
// enemy collision from sides with Bounce back effect if hit without attacking
else
{
oPlayer.hpamount-=AttackDamage(attack,global.armor)
//Knockback
if place_free(x+24,y) && place_free(x-24,y)
{
x+= 24 * sign(x - oPlayer.x);
y-= 3;
}
}
}
STEP 2 Enemy Projectiles Damages You
Battle system when hit by Enemy arrow
Some of your enemies also have ranged attack so these object must do damage to you as well. So we will add collision events in your characters object with all objects that can hit you for damage.
Create Event for all enemy projectiles
attack=5 // Change to be higher than your armor
Collision Event with all Projectiles
with (other) instance_destroy();
damage=AttackDamage(other.attack,global.armor);
hpamount-=damage;
For any projectile an enemy attacks you with you need to add collision
STEP 3 Attack variable
All these objects must have an attack variable in the create event
You should make the attack greater than your armor or the object will miss you. For the rock make the damage higher so it damages you more.
Create Event of projectile objects you pick the attack strength
attack=15; // this should be greater than your armor
Viking axe after erasing the Viking body
STEP 4 AI BOSS
Battle system with AI Boss
Your AI boss monster has to have a slightly different attack. He attacks with his ax so we need to make a separate object for his ax just like we did with your player.
To do this we have to make a new object called oVikingAxe with a new sprite that only contains the Ax but not your vikings body.
Duplicate the viking attack sprite erase out all the body from each frame of the animation and center it.
As you can see from the pictures only the some of the frames have an ax in them
Remember to keep all the blank animations in the new sprite so the two animations sync.
Now we need to open your oViking object and make it create an axe attack when in a state of attacking
Boss Creates Ax Attack Object
Now we need to open you oViking object and make it create an ax attack when in a state of attacking
Find the switch statement in the viking object and change what happens when the viking is in a state of "attack"
Boss Ax Object damages you
Now we need to make the ax object do damage to you it will be the same thing that happens when your spear hits an enemy.
Damage the AI Boss with your spear and arrows
Now we need to make you damage the boss this will be done just like the other monsters so we can do this by making the boss a child of the parent monster which in my case is the wolf.
Create Event of oVikingAxe
attack=25 // this should be greater than your armor
Animation End Event oVikingAxe
instance_destroy();
Draw Event oVikingAxe
draw_sprite_ext(sprite_index,-1,oViking.x,oViking.y,image_xscale,1,0,c_white,1);
Step Event for oViking
case "attack":
// attack state
if canattack==1 && !instance_exists(oVikingAxe)// No ax attack object can exsist
{
canattack=0;
var ID;
ID = instance_create_layer(x,y,"Instances",oVikingAxe);
with (ID)
{
image_xscale = oViking.image_xscale; // makes Ax same direction as viking
}
scr_attack(attackSprite);
}
break;
Collision event with oVikingAxe in your player
if canHit
{
canHit = false;
alarm[0] = 15;
damage=AttackDamage(other.attack,global.armor);
hpamount-=damage;
}
Alarm event in oPlayer
canHit=true;
STEP 5 Push-able Blocks
Now we will create a push-able block.
Create an object called oPushBlock and give it a sprite
Make this object solid and make its parent the oBlock
Notice the abs in front of oPlayer.key_left
abs means absolute value so the value of key_left will always return a positive. We have to do this because key_left in the player object has a negative added to make it match movement.
Therefore we have to check the abs of key_left.
Step Event of oPushBlock
// Move Blocks if player is near and pressing key
if place_meeting(x-1,y,oPlayer)&& oPlayer.key_right
{
x+=global.movespeed;
}
if place_meeting(x+1,y,oPlayer) && abs(oPlayer.key_left)
{
x-=global.movespeed;
}
if (place_free(x+0,y+1))
{
gravity_direction=270;
gravity=0.5;
}
else
{ gravity_direction=270;
gravity=0;
}
if (vspeed>12) // Limits fall speed
vspeed=12;
Collision with oBlock
move_contact_solid(270, -1); // Makes pushable block stop when it hits a block
vspeed=0;
Side to Side Platform
STEP 6 Moving Platforms Side to Side
Moving platforms are a great addition to a platform game. It allows your character greater movement allowing you to ride a platform like an elevator up or down or side to side.
First start by creating an object for your platform.
We will use nice looking sprite for this object.
Create an object with this sprite and call it oS2s
Make this object a child of you oBlock so you can walk on it.
A Cosine graph is a wave that dips below and above zero so it has positive and negative points this makes it a great function to make things move smoothly left and right without any collisions.
This code also does something unique it will turn off the mask of the platform when your player is below it and only turns it on when the player is above it.
This allows you to jump up through these platforms and land on them.
Create Event for oS2s
angle = 0;
angle_increment = 0.017453292519943295769236907684886; //Radian constant
angle_multiplier = 1.5;
sprite_index=-1;
Step Event for oS2s
angle+=angle_increment
x += cos(angle) * angle_multiplier; // makes platform move right to left
if instance_exists(oPlayer)
{
if (round(oPlayer.y +(oPlayer.sprite_height/2)) > y+1)
mask_index=-1;
else
mask_index=sS2s; // turns sprite mask on
if place_meeting(x,y-1,oPlayer) // checks if you are 1 pixel above the platform
{
oPlayer.x+=(cos(angle) * angle_multiplier) // adds speed of platform to your character
}
}
Draw Event for oS2s
draw_sprite(sS2s,-1,x,y)
STEP 7 Change Origin of Side to Side sprite
The origin of this block is very important so it needs to be x=32 y=1
STEP 8 Moving Platforms Up and Down
Create another object and call it oPlatUpDown
Give it a sprite and center its origin
We will move this in a similar way as the left right platform using a Sin wave
Make this object a child of you oBlock so you can walk on it.
This platform can squish you into the ground so place them wisely
Create Event of oPlatUpDown
// Initilize varibles
movechar=0
platSpeed=0
angle = 0;
angle_increment = 0.017453292519943295769236907684886; //Radian constant
angle_multiplier = 1.5;
Step Event for platform object
// makes platform move up and down
angle+=angle_increment
platSpeed=sin(angle) * angle_multiplier;
if (place_meeting(x,y-1,oPlayer))
{ //move the character if it's resting on the platform
movechar=1
}
else
movechar=0
if movechar
{
oPlayer.y+=platSpeed
}
if place_meeting(x,y+platSpeed,oPlayer)
{ //same with y values
if oPlayer.y<y // Makes character stable on platform
oPlayer.y=y+platSpeed+(sprite_height/2+oPlayer.sprite_height/2)*-1
else
oPlayer.y=y+platSpeed+(sprite_height/2+oPlayer.sprite_height/2)
}
y += platSpeed
Draw Event of up down platform
draw_sprite(sUpDown,-1,x,y)
STEP 9 Walking up Hills
We can also add to your game by giving you the ability to walk up and down hills. To do this we must create an uphill and a downhill block object.
Make this object solid and a child of your oBlock.
To make you walk uphill we will check if a place is empty to the right and above if so we move right and up or diagonally. It is easy and works pretty well. I used these slope sprites.
Add the code above the jumping code.
Step event of oPlayer
//Slope movement Left
if place_empty(x-4,y-6) && keyboard_check(ord("A"))
{
x-=4
y-=6
move_contact_solid(270,6)
}
//Slope movement Right
if place_empty(x+4,y-6) && keyboard_check(ord("D"))
{
x+=4
y-=6
move_contact_solid(270,6)
}
STEP 10 Crumbling Platform
We can also create a one way path that falls apart after you walk on it. This is a nice addition to a room that will allow you to create a one-way-path. After you walk across no way back. You could use this as a path to the boss room so you must fight the boss with no way to escape. Call this sCrumble and the mask sprite sCrumbleMask
Make this object a parent of the oBlock and also make is solid
You also need to change the collision mask when you step on it this allows it to fall without getting stuck on the crumble blocks own corners. So duplicate the crumble sprite and modify the mask of the new sprite to be Manual and Ellipse instead of Automatic Precise.
Create Event for oCrumble
fall=0;
Draw Event for oCrumble Game Maker Studio
draw_self();
Step Event for oCrumble
if place_meeting(x+sprite_width,y-1,oPlayer)
{
fall=1;
sprite_collision_mask(sprite_index, false, 0, 0, 0, 0, 0, 2, 0);
}
if fall=1
{
image_angle+=vspeed;
if !instance_place(x,y+1,oBlock)
gravity=1
if instance_place(x,y+1,oBlock)
{
gravity=0
vspeed=0
}
if vspeed>10
vspeed=10
}
STEP 11 Menu System (GUI)
Now we want to display things in you game like score, health run speed, jumping, weapon and anything else you can think of.
This is the first part to making your game scale-able, or up-gradable.
To make room for the GUI we need to increase your room size by 100 pixels so change the height from 512 to 612
Create a new object called oGui gui stands for graphical user interface.
You may have noticed we used some variables that have not been used yet. Now we will use them in the GUI.
The gui has no sprite and only a Draw GUI Event.
However the GUI will draw sprites for your weapons that you will use.
We will also draw panel sprites to make it look better Save all the sprites below or make your own to use for your GUI just keep the dimensions of your blocks 100px x 100px and 100px x 200px
Draw Event oGui
draw_set_font(font_kenny);
draw_set_halign(fa_left);
// Draws UI Sprite Panels
draw_sprite(sBluePanel,-1, 0,room_height-100);//panel for HP,score,speed,jump
draw_sprite(sGreyPanel,-1,200,room_height-100);//panel for attack armor
draw_sprite(sBluePanel,-1,300,room_height-100);//panel for weapon used
draw_sprite(sGreyPanel,-1,500,room_height-100);//panel for helm
draw_sprite(sGreyPanel,-1,600,room_height-100);//panel for armor
draw_sprite(sGreyPanel,-1,700,room_height-100);//panel for shield
draw_sprite(sGreyPanel,-1,800,room_height-100);//panel for belt
draw_sprite(sGreyPanel,-1,900,room_height-100);//panel for boots
draw_sprite(sInventory,-1,1012,room_height-80);
draw_sprite(sForge, -1,1012,room_height-20);
// Draw player statistics
draw_text(10,room_height-90,"Score: "+string(score));
draw_text(10,room_height-70,"Health: "+string(oPlayer.hpamount)+"/"+string(oPlayer.hpmax));
draw_text(10,room_height-50,"Speed: "+string(global.movespeed));
draw_text(10,room_height-30,"Jumping: "+string(global.jumpspeed));
//Draw Attack armor sprites
draw_sprite(sGuiAttack,-1,220,room_height-70);
draw_sprite(sGuiArmor,-1,220,room_height-30);
//Draw attack & armor
draw_set_font(font_kennyLarge);
draw_text(250,room_height-90,string(global.attack));
draw_text(250,room_height-50,string(global.armor));
//Draw weapon used sprites
draw_sprite(sGuiBow,-1,333,room_height-50);
draw_sprite(sGuiSpear,-1,367,room_height-50);
//Draw weapon selected checkmark
if oPlayer.wep="bow" draw_sprite(sCheck,-1,333,room_height-50)
if oPlayer.wep="spear" draw_sprite(sCheck,-1,367,room_height-50)
//Draw armor
draw_sprite(sHelm,global.helmlevel,550,room_height-50);
draw_sprite(sArmor,global.armorlevel,650,room_height-50);
draw_sprite(sShield,global.shieldlevel,750,room_height-50);
draw_sprite(sBelt,global.beltlevel,850,room_height-50);
draw_sprite(sBoots,global.bootslevel,950,room_height-50);
The armor sprites will be used latter in the forging system.
To make sure that the GUI is drawn above other items in your room change the depth to -100
Remember positive depths are farther from you and negative are closer to you.
You will need to create two fonts one size 12 and one size 28
Put this object in your rooms
GUI Code Break down 1-14
All that code looks overwhelming so we have to look at it in chunks
Lines 1 - 5 does the following
Sets font to the size 12 font
Aligns font left
Draws all the panels
Draws GUI event draws all the items on a different view the items on the view stay in place even if your character moves to a new position in the room. Think of a view as another layer that is placed on top of your room your room may move but the GUI view will not.
The GUI is comprised of 8 different sprite panels Two large once and 6 smaller.
The smaller panel is 100px x 100px and the larger is 200px x 100px
So they are easy to position.
The draw_sprite() function positions each of these panels. The x value goes from 0 to 200 and so on.
The Y value uses the room_height to position it that way no matter what size you make your room it will always be at the bottom of the screen.
GUI Code Break down 16-20
Lines 16 - 20 does the following
Draws Score, Health, Run Speed, Jumping
Notice the draw_text can draw more than one item at a time. For example line 18
"Health: "+string(oPlayer.hpamount)+"/"+string(oPlayer.hpmax))
This will draw the words inside the "" then draw the value of the variable hpamount which is located in oPlayer
Line 19 is needs to be changed to draw the value of two variables that are multiplied together
string(oPlayer.movespeed*oPlayer.movefactor))
This way when your player is temporarily slowed his speed changes in the GUI as well
GUI Code Break down 22-29
Lines 22- 29 does the following
Draws Each weapon sprite with the following:
Name of weapon
Sprite of the weapon
Cost to use weapon
On line 23 it will draw the value of thirdweapon. So it will say Unknown until you get the wand then it will change to Wand of Fire
We will talk about the wand of fire later as a boss drop.
Line 24 checks if you have the wand remember when you collide with the wand object the havewand variable changes from 0 to 1.
This triggers the drawing of the wand sprite and the cost to use the wand.
GUI Code Break down 31-37
Lines 33- 47 does the following
Draws a rectangle around the selected weapon
GUI Code Break down 39-44
Lines 33- 47 does the following
Draws a rectangle around the selected weapon
STEP 12 Monsters Drop Items in the Game
Now we will add Monster drops in our game, you may remember the oStart object in contains information about these items in array variables.
These include forging gems for each of your 5 armor pieces and monster loot we need sprites for each of these items. Use a single sprite with sub-images.
These Items will do the following:
Spirit Potion: Gives you full Energy
Health Potion: Gives you full Health
Shield Amulet: Gives you a shield for a short time
Key: Opens doors
Arrow: Gives you arrows to shoot
Gold: Gives you gold
We will create these objects dynamically using an array. In fact we will create only one object but it can look like any of the 6 items. If you want more items make the sprite larger
First create a sprite with all the animations contained inside of it it should look like this>
Name this sprite sLoot
Now create an object with this sprite and call it oLoot
We will put some code is this item latter but first we need to make our script.
STEP 13 Create Drop Script
We are going to create our first script. You are beginning to notice some of the step events are quite large and hard to debug so it is often necessary for you to create your own functions to do a specific task, or to condense a large section of code into a more manageable chunk, or even to be able to re-use a particular code block in many different places. That is why Game Maker and Studio gives you the ability to create scripts.
Our first script will calculate what item a monster will drop when it is destroyed. Call this script Drops
Scripts are called from any object they then run some code and return a value.
Scripts call also be given arguments. Arguments are values that are passed to the scripts which can be used by the code in the script. Our scripts does not use arguments.
This script does the following:
Picks a random integer from 1 to 100
Looks through a list of if statements to determine what value to pass back to the object that called the script
Returns that value to the object
This value is the subimage used to identify the object
I created a drop chance table:
0 Gold 70% chance to drop
1 Arrow 10% chance to drop
2 Key 5% chance to drop
3 Health Potion 5% chance to drop
4 Energy Potion 5% chance to drop
5 Amulet of Shield 5% chance to drop
Drops Script
// Drops() no arguments sent
randomize();
i=irandom_range(1,100);
// Random Drops
if i < 70
return(0) // Gold Drop 70%
if i>70 && i<=80
return(1) // Arrow drop 10%
if i>80 && i<=85
return(2) // Key drop 5%
if i>86 && i<=90
return(3) // Health Potion drop 5%
if i>91 && i<=95
return(4) // Energy Potion drop 5%
if i>96 && i<=100
return(5) // Amulet drop 5%
return(-1)
STEP 14 oLoot Create Event
When an item is created it will run the script to figure out what type of item to drop. Add this code to the create event of oItem.
The mouse enter and mouse leave events check to see if your mouse pointer is inside the sprite. If it is inside of the sprite we will draw a text message for what item was dropped.
The draw event also use the variable ii to draw the correct sprite frame that corresponds to the drop the script sent back.
Create Event for oLoot
ii=Drops();
if ii==-1
instance_destroy();
gravity=1
direction=270
draw=0;
Draw Event of oLoot
draw_sprite(sLoot,ii,x,y);
draw_set_font(font_normal);
if draw==1
{
draw_set_color(c_black);
draw_text(x-16,y-34,string(global.loot[ii,0]));
}
Mouse Enter Event for oLoot
draw=1;
Mouse Leave Event for oLoot
draw=0;
Collision Event with oBlock
vspeed=0;
gravity=0;
STEP 15 Make monsters drop items
The monsters will drop items when they are killed so we go to your parent monster which in my case is the snake.
In the destroy event the parent monster add this code. This will create a drop whenever a monster is destroyed.
Destroy Event in oWolf
score+=points; // Add points for killing monster
instance_create_layer(x,y,"Instances",oLoot); // creates a loot drop
STEP 16 Picking Up Dropped Items
Add a collision event with oLoot in your character and add this code.
The code will find out what type of item the loot is based upon the ii variable.
That variable will be a number from 0-5. Remember each number corresponds to the sprite sub-image.
Collision Event in oPlayer
with other
instance_destroy() // destroys the loot
global.loot[other.ii,1]+=1;// Adds one of the loot to your inventory
STEP 17 Inventory
Will will add one to your inventory each time you collect an item. We will add a inventory to the GUI latter.
Arrows as Ammo for character
Now that we have a variable that keeps track of how many arrows you have lets make your character NOT be able to shoot if he or she has no arrows. This is done quite easily. The variable that stores the number of arrows is:
global.loot[1,1]
We set this variable to be equal 100 so when you start the game you have a total of 100.
Go the your characters Animation End event and change the code to this:
The Code will check if you have any arrow if so shoot an arrow and deduct 1 from your total arrows.
Inventory Pop Up GUI
Now we will make another GUI that will pop up when we press the letter I.
This is called a pop-up inventory. This will display all the loot that you have collected.
I will show you in the GUI how to draw items using a for loop.
This works as follows - First statement1 is executed, then the expression is evaluated and, if it is true, statement 3 is executed. Then statement 2 and then the expression is evaluated again. This loop will continue until the expression is found to be false.
Now, this may sound complicated when written like that, but you should interpret it as:
The first statement initializes the for-loop.
The expression tests whether the loop should be ended.
Statement2 is the "step" statement that goes to the next loop evaluation.
This extremely useful for doing repetitive tasks that would involve multiple lines of code in any other way, and is commonly used as a counter for evaluating arrays, or drawing things. the following code example illustrates a typical use for this type of statement:
Animation End Event for oPlayer
switch state
{
case "attackbow":
//When you create the arrow
if global.loot[1,1]>0 // checks if you have arrows to shoot
{
global.loot[1,1]-=1 // subtracts one from total arrows
var ID;
ID = instance_create_layer(x,y,"Instances",oArrow);
with (ID)
{
image_xscale = other.image_xscale; // makes arrow face same direction as you
hspeed = other.image_xscale*15; //Sets the arrow's speed to 15 or -15
}
}
break;
case "attacksword":
break;
}
canattack=0;
state="idle";
Create Event of oInventory
inv=0;
Step Event of oInventory
if keyboard_check_pressed(ord("I"))
inv=!inv
Draw Event of oInventory
draw_set_font(font_small);
draw_set_halign(fa_center);
draw_set_color(c_black)
// Draws UI Sprite Panels if you press I
if inv=1
{
var i;
for (i = 0; i < 20; i += 1) // Loop to Draw 20 panels for inventory
{
draw_sprite(sInvPanel,-1,view_xview[0]+ 50*i,view_yview[0]+view_hport[0]-150);//panel loot drops
if i<=5
{
draw_text_ext(view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-150,string(global.loot[i,0]),-1,50);
draw_sprite(sLoot,i,view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-125);
draw_text(view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-115,string(global.loot[i,1]));
}
}
}
STEP 18 Sprite for inventory
Save this sprite for the panels sprite
Create an object called oInventory it has no sprite make the depth -100
Create a new font called font_small size 8
STEP 19 Forge Item
Now we will make the forge item drop. This item is a very rare drop you can place it in locked chests or a drop from a boss monster.
These items are the materials the the forging smith will use to upgrade your armor. This NPC shop will come later.
This item will use much of the same code as the oLoot so make it a child of the loot object.
These are the items save this sprite sForgeDrop
Gold Helmet
Red Breast Plate
Green Shield
White Belt
Blue Shoes
Since the create event and draw event are different than the parent we need to fix that. The Create Event will choose which gem to drop.
Create Event of oForge
ii=choose(0,1,2,3,4) // this will randomly select one of the 5 forging gems
/*
0 Gold Helmet Gem
1 Red Breast Plate Gem
2 Green Shield Gem
3 White Belt Gem
4 Blue Shoes Gem
*/
gravity=1
direction=270
draw=0;
Draw Event for oForge
draw_sprite(sForgeDrop,ii,x,y);
draw_set_font(font_normal)
if draw=1
{
draw_set_color(c_black)
draw_text(x-16,y-34,string(global.forge[ii,0]))
}
STEP 20 Picking Up Forge Drops
Add a collision Event with the oForge and add the code to add it to your inventory.
oInventory code for forging Gems
Replace the draw code with the new inventory code. This will show how a nested loop works. This code will draw the forging gems on the inventory pop up GUI. It also shows how to change font size when using a draw event. This can be changed to display up to 20 items in the inventory just increase the size of the for loop, add more sub-sprites and add to the global.loot or global.forge variables.
Collision Event for in your player with oForge
with other
instance_destroy() // destroys the loot
global.forge[other.ii,1]+=1;// Adds one of the forge gems to your inventory
Draw Event oInventory
draw_set_halign(fa_center);
draw_set_color(c_black)
// Draws GUI Sprite Panels if you press I
if inv=1
{
var i;
for (i = 0; i < 20; i += 1) // Loop to Draw 20 panels for inventory
{
if i<=5
{ //Draws Loot drops
draw_set_font(font_small)
draw_sprite(sInvPanel,-1,view_xview[0]+ 50*i,view_yview[0]+view_hport[0]-150);//panel loot drops
draw_text_ext(view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-150,string(global.loot[i,0]),35,50);
draw_sprite(sLoot,i,view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-125);
draw_set_font(font_normal)
draw_text(view_xview[0]+23+50*i,view_yview[0]+view_hport[0]-133,string(global.loot[i,1]));
}
if i>5 && i<=10
{ //Draws Forge Gems
draw_set_font(font_small)
draw_sprite(sInvPanel,-1,view_xview[0]+ 50*i,view_yview[0]+view_hport[0]-150);//panel loot drops
draw_text_ext(view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-150,string(global.forge[i-5,0]),35,50);
draw_sprite(sForgeDrop,i-5,view_xview[0]+25+50*i,view_yview[0]+view_hport[0]-125);
draw_set_font(font_normal)
draw_text(view_xview[0]+23+50*i,view_yview[0]+view_hport[0]-133,string(global.forge[i-5,1]));
}
}
}
STEP 21 Talk to NPC
We will now allow your player to talk with NPC (non-player characters)
These characters can talk to you, give you game hints, give you quests, or be a shop keeper.
I will show you how to talk with one of them first.
Create a new object for the princess.
Add a create event for the dialog that the NPC will say.
Also add the draw event to draw the message in a call out box
Use this sprite for the call out sprite
Notice that the dialog is typewriter text which looks cool.
Create Event of oPrincess
//messages in dialog with princess
i=-1
message[0]="Hello, my name is Sansa" // Intro
message[1]="I need some help, can you help me?# Y or N"; // Ask for help
message[2]="Good. An evil giant has stolen my family wand."; // Yes Answer
message[3]="OK, I thought you liked adventure I guess I was wrong";// No Answer
message[4]="The evil giant lives in the dungeon you will need lots of arrows, His sword attack is very powerfull beware."; //Quest Details
message[5]="Please Hurry. Defeat the evil giant!";// Quest not finished
message[6]="Great job. I will reward you by teaching you to use the wand."; //Quest finished
message[7]="You are a true hero I will never forget you."; // Thank you hero
maxdialog=7
//states of the princess
state="";
myPos=0
questFinsished=false
Draw Event for princess
draw_set_color(c_black)
draw_set_font(font_small)
draw_set_halign(fa_left)
//In the Draw Event
draw_sprite(sPrincess,0,x,y)
if state=="talk"
{
draw_sprite(sTextBox,-1,x,y-50)
var myStr,copyStr;
myStr = string(message[i])
copyStr=string_copy(myStr,1,myPos)
draw_text_ext(x-90,y-70,copyStr,10,190)
myPos+=0.5 //Number of characters revealed per step (0.5 for one every two steps)
}
STEP 22 Princess Right Released Event
To be able to talk to the Princess we will right click on her. You could change this to left click if you want it doesn't matter the code is the same.
In the code it will cycle through the message[0] variable and display the correct dialog.
The dialog will go in a certain order
Intro
Ask for help
Yes Answer
No Answer
Quest Details
Quest not finished
Quest finished
Thank you hero
This will trigger a series of things that will happen in the Draw Event we will talk about that next.
Right released Event for princess
state="talk"
myPos=0
switch i
{
case -1:
i=0
break;
case 0:
i=1
break;
case 1:
i=1;
break;
case 2:
i=4;
break;
case 3:
i=1;
break;
case 4:
i=5;
break;
case 5:
if questFinsished==true
i=6;
break;
case 6:
i=7;
break;
case 7:
i=7;
break;
}
if i>maxdialog
i=maxdialog;
Step Event of Princess
switch i
{
case 1:
{
// Ask for help
if keyboard_check(ord("Y"))
{
myPos=0;
i=2;
}
if keyboard_check(ord("N"))
{
myPos=0;
i=3;
}
break;
}
}
keyboard A and Keyboard D events to turn off dialog
state="";