Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> [GM][TAW]Item Swapping
HeroOfHyla
post Jun 8 2009, 10:15 PM
Post #1


Twirling towards freedom
Group Icon

Group: +Gold Member
Posts: 2,791
Type: Scripter
RM Skill: Advanced




Item Swapping
Main TAW Topic: http://www.rpgrevolution.com/forums/index....showtopic=31232
This tutorial will teach item swapping - you can carry 1 item, and you can swap your item for an item on the ground.

There are fewer screenshots in this one, because it uses more coding and less eventing.

First things first, make a character sprite, and several item sprites. I'm going to be using 4 items: a sword, a hammer, a key, and a gun.

I'm not going to walk through basic movement again, but you can use this script if you don't have a pre-made movement script:
CODE
{
    //because the origin point of the sprite is based on a pixel and not an intersection, x-13 is one pixel to the left of the image, and x+12 is one to the right.
    //if it were an intersection, you'd use 13 for both
    if (keyboard_check(vk_up))
    {
        if (!collision_rectangle(x-12,y-13,x+11,y-13,objBlock,0,1))
        {
            y -= 4;
        }
    }
    if (keyboard_check(vk_down))
    {
        if (!collision_rectangle(x-12,y+12,x+11,y+12,objBlock,0,1))
        {
            y += 4;
        }
    }
    if (keyboard_check(vk_left))
    {
        if (!collision_rectangle(x-13,y-12,x-13,y+11,objBlock,0,1))
        {
            x -= 4;
        }
    }
    if (keyboard_check(vk_right))
    {
        if (!collision_rectangle(x+12,y-12,x+12,y+11,objBlock,0,1))
        {
            x += 4;
        }
    }
}

(replace the 11,12,and 13 values based on your sprite. They should be the size of your sprite divided by 2, plus or minus 1. So if your sprite is 32x32, use 15,16, and 17)

Anyway, go ahead and make an object with your character sprite that runs this script in the step event.
Also make an object called objBlock that uses a simple square solid colored sprite. Set it to solid, and be sure to make sure the sprite is not transparant.

There are a bunch of different ways to set up item slots for the character. The method we're using is to have 2 variables, item and subItem. Those will hold the IDs of the objects that are being held. You will be able to define as many objects as you want without needing to adjust the code for swapping them. In the create event for the character object, set item and subItem to -1. Don't use 0, because it's the ID of an object.

Let's stop working on the character for a bit, and start defining a few items. The very first thing we want to do is make an object that will be the parent of all other objects. Make one called objParentItem. You don't need to do any coding in this object.

Make another object, let's start with objKey. Name it, and assign it a sprite. Set its parent to be objParentItem. This means that any time you do a check relating to objParentItem, objKey will also be considered.

Make a few more objects by duplicating the objKey, and changing the name and replacing the sprite. I wound up with 4 total.

Now we need to decide what key to use for swapping items. I'm going to use SPACE. Add a key pressed: space event to the character object. Add a "Check Object" action for this event. Tell it to check for objParentItem at position x,y. Now, because objKey is a child of objParentItem, if there's an objKey at the position, that will make it return true.

The fastest way to do this next stuff is with a script:
CODE
{
    colliding = collision_rectangle(x-16,y-16,x+15,y+15,objParentItem,0,1);
    if (colliding >= 0)
    {
        if (item != -1)
        {
            instance_create(x,y,item);
        }
        item = colliding.object_index;
        with (colliding)
        {
            instance_destroy();
        }
    }
}


What this does is check what object the character is touching, and set the item to that. It also drops the old weapon on the ground if there was one.

Try it out, and you should be able to get it to work! If you want to experiment some, consider adding a second item slot, or items that use ammo! Also, consider having the game draw the name of the object currently being held.



DEMO:
http://www.mediafire.com/download.php?ud1nmymzmyl


__________________________
Asexual - Arizonan - Atheist
I've got a gmail account and a yahoo mail account under this name, if you need to contact me.
You can find me on facebook too. facebook.com/heroofhyla . If you send me a friend request, tell me who you are so I don't get confused.

PlagueRPG SITE IS WORKING! (if you see "test page," hit ctrl+f5)
Latest update: New battle system demo, April 19, 2010

http://heroofhyla.deviantart.com/ ----- http://heroofhyla.livejournal.com/ ----- http://thatonecomic.smackjeeves.com
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 18th May 2013 - 10:52 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker