RPG-Inventory - Drag and Drop

Index

Stats

4,703 visits, 12,911 views

Translations

This tutorial hasn't been translated.

Tools

Bit Logic

To check, if our gauntlet may fit on the hand, we'll do a bitwise AND with these two numbers 5 AND 1. And big suprise, the result will be 1 which is equal to true!!

Why does that work?

The bitwise AND works like this:

1 AND 1 = 1

0 AND 1 = 0

1 AND 0 = 0

So if we have 101 (our slot) and 001 (the gauntlet) this will happen:

1. Bit - 1 AND 0 = 0

2. Bit - 0 AND 0 = 0

3. Bit - 1 AND 1 = 1

The result is 1 and one is equal to true - just like 0 is the "same" as false

Now, how do we set the bits? There are two ways, one is adding the decimal values of the "position" the bit is in, the other is to combine bit groups with the binary OR.

To make it easy on you, I have provided the decimal values you need as instance variables within the slot sprite - even thou this example does not use this variables.

If you want to create a hat, that you could store in the bag as well as hold it in the hand or wear it on the head, you'd have 49.

1 (for the bag - or all), 16 (for the hand) and 32 for (for the head)

We don't use the bitwise OR in this example, but to be complete here's how it works:

1 OR 1 = 1

0 OR 1 = 1

1 OR 0 = 1

0 OR 0 = 0

You see, that the OR sets the bit, if it is not already set.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!