Easy Code: Item Merging Game

esterD

  • Posts: 3
Hello All!  My name is Ester. I have set out to create an Item Merger Game. At first I had no idea how to do this. It seemed pretty complicated. Spent many frustrating hours deleting codes. Then, it worked! I wanted to save you all the trouble since the code needed is so amazingly simple and I have seen unanswered posts on the forums before about how to do this. So, Here it is...

HOW TO CREATE AN ITEM MERGER: With Two Blocks of Code

First create a Drag & Drop Behavior with This Event:

Imput/Universal: On Actor (See attachment: event1.png  -You can ignore the if statements, they link to other functions in my game.)
When the mouse is (dragged)
set (x) to (((x) of camera) + ((x) of (mouse))) for (self)
set (y) to (((y) of camera) + ((y) of (mouse))) for (self)

Next, add this behavior to any object you want to drag around the scene. Next, add an instance of the following Event for every merge combination you need on each Actor:

Collissions: Type & Type (see Attachment: event2.png)
when a (Type1) hits a (Type2) --
create (Type3) at ( x: ((x) of (1st actor)) y: ((y) of (1st actor) ) at (Front)
kill (1st actor)
kill (2nd actor)

If this code doesn't work, I would make sure your collisions are setup properly.  Ensure everything you need to collide is in a compatible collisions group. Also, if you are using collision boxes other than square, make sure to set your Game Settings/Settings/Advanced/Physics to Box2D. (see Attachment settingsPanel.png)  You may be having failed collisions because Simple Physics only allow square collision boxes.

I hope this is helpful. Let me know if you get any good ideas for Item Merger games or how to simplify this code even more.
Have fun creating! :D

« Last Edit: January 03, 2020, 11:55:07 pm by esterD »

SadiQ

  • Posts: 1790
You can also use a distance check instead of a collision event to let you know when you can 'merge' your item. Useful in card games where you want to snap the card to a position when you get it close enough.
Glad to see people trying to learn how to do things instead of relying on premade behaviors (there is a drag and drop one).
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

esterD

  • Posts: 3
Ooh I am going to try that because I am actually having some troubles with the code I wrote above. LOL Thanks!

esterD

  • Posts: 3
I didn't find a way to check for distance, where is that located?

SadiQ

  • Posts: 1790
There is no block for that, you must calculate that distance yourself using the Pythagorean theorem. If you need help drop a line here :)
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.