Hittest On Draggable Block

I know there’s alot of hittest questions/answers out there but I can’t find anything to modify for my needs.

I have a few blocks on stage (26) and they are all the same size and are Movie Clips, each block represents a letter.
I want to be able to drag the blocks up/down left/right in order to make a word, But I don’t want them to overlap as I’m dragging them.
So I would need to move one out of the way to get the other in that place.

I am asking If anyone can help me get started on two of the blocks (LetA & LetB),
I have a drag action on each block but I’m guessing that I need a Hittest code so If “LetA” hits “LetB” It won’t overlap, It will stop moving In that dirrection but still move in another.

Thanks.

hey man, I’m doing a similar thing at the moment being squares for navigation, which cant leave an area and cant touch eachother. My code which I’ve just finished, doesnt seem to work, but maybe someone else can point out what ive done wrong.

Anyways the code:


onClipEvent (enterFrame) {
this.onPress = function(){
    this.startDrag(); 
}
cantleave = _root.navarea.na;
var canttouch:Array = new Array();

// define the other objects that cant be touched
canttouch[0] = _root.nav_services;
canttouch[1] = _root.nav_portfolio;
canttouch[2] = _root.nav_about;
canttouch[3] = _root.nav_contact;


    if (this._x < cantleave._x){
        this.stopDrag();
        this._x += 16;
        this.rewind = true;
    }else if (this._x > (cantleave._x + cantleave._width)){
        this.stopDrag();
        this._x -= 16;
        this.rewind = true;
    }else if (this._y < cantleave._y){
        this.stopDrag();
        this._y += 16;
        this.rewind = true;
    }else if (this._y > (cantleave._y + cantleave._height)){
        this.stopDrag();
        this._y -= 16;
        this.rewind = true;
    }

    var i:Number;
for (i = 0; i < 4; i++) {
    if (this.hitTest(canttouch*)){
    if ((this._x + this._width) == canttouch*._x or (this._x + this._width) > canttouch*._x and (this._x + this._width) < (canttouch*._x + (canttouch*._width/2)) ){
        this.stopDrag();
        this._x -= 16;
        this.rewind = true;
    }else if (this._x == (canttouch*._x + canttouch*._width) or this._x > (canttouch*._x + (canttouch*._width/2)) and this._x < (canttouch*._x + canttouch*._width)){
        this.stopDrag();
        this._x += 16;
        this.rewind = true;
    }else if (this._y == (canttouch*._y + canttouch*._height) or this._y < (canttouch*._y + canttouch*._height) and this._y > (canttouch*._y + (canttouch*._height/2))){
        this.stopDrag();
        this._y -= 16;
        this.rewind = true;
    }else if ((this._y + this._height) == canttouch*._y or (this._y + this._height) > canttouch*._y and (this._y + this._height) < (canttouch*._y + (canttouch*._height/2))){
        this.stopDrag();
        this._y += 16;
        this.rewind = true;
    }
    }
}


    
this.onRelease = function(){ 
    this.stopDrag();
    _root.navtext.textbox = "";
    
    if (this.hitTest(_root.navarea.dotbox)) {
        this._x = (_root.navarea.dotbox._x + 18);
        this._y = (_root.navarea.dotbox._y + 18);
        _root.navtext.textbox = "HOME";
        this.rewind = true;
        _root.docked = "HOME";
    }
}
}

Hi Neolumi,
This must be tougher then I thought because I’ve been stuck on this going on 4 days now,
I have asked In about 3 different places but no one seems to know how to do It because I’ve recieved no replies on It.

Im not 100% sure on what you want to achieve. But i was bored, so i made this little script, to show you abit about how you can manage more complex dragging, without the start and stopDrag methods.

This code, you can paste in keyframe 1 in an empty movie, and just publish it. Buff up framerate for leetness :cap:

This script drags one block along the x-axis, and if it hit the other block, it will swap to drag the other block. This is probably not exactly what you intend to do, but im fairly sure the code will give you the hint on how to solve your exact problem.


this.createEmptyMovieClip("square1", this.getNextHighestDepth());
square1._x = square1._y = 100;
square1.beginFill(0xFF0000);
square1.moveTo(0, 0); square1.lineTo(50, 0); square1.lineTo(50, 50);
square1.lineTo(0, 50); square1.lineTo(0, 50); square1.endFill();
//
this.createEmptyMovieClip("square2", this.getNextHighestDepth());
square2._x = 200; square2._y = 100;
square2.beginFill(0x000000);
square2.moveTo(0, 0); square2.lineTo(50, 0); square2.lineTo(50, 50);
square2.lineTo(0, 50); square2.lineTo(0, 50); square2.endFill();
//
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    if (square1.hitTest(_xmouse, _ymouse, false)) {
        difference_x = square1._x-_xmouse;
        drag = true;
        toDrag = square1;
    } else if (square2.hitTest(_xmouse, _ymouse, false)) {
        difference_x = square2._x-_xmouse;
        drag = true;
        toDrag = square2;
    }
};
mouseListener.onMouseMove = function() {
    if (drag) {
        if (toDrag == square1) {
            if (square1.hitTest(square2)) {
                square1._x = square2._x-square1._width;
                if (square2.hitTest(_xmouse, _ymouse, false)) {
                    square2._x++;
                    difference_x = square2._x-_xmouse;
                    toDrag = square2;
                }
            } else {
                square1._x = _xmouse+difference_x;
            }
        }
        if (toDrag == square2) {
            if (square2.hitTest(square1)) {
                square2._x = square1._x+square1._width;
                if (square1.hitTest(_xmouse, _ymouse, false)) {
                    square1._x--;
                    difference_x = square1._x-_xmouse;
                    toDrag = square1;
                }
            } else {
                square2._x = _xmouse+difference_x;
            }
        }
    }
};
mouseListener.onMouseUp = function() {
    drag = false;
};
Mouse.addListener(mouseListener);

No comments is applied sorry, but you cant get all for free :wink:

Cheers :beer:

Hi Bobocop and others…

I’ve had a quick go for you (an hour or so) and whilst I’m pretty sure that this isn’t what you’ve asked for (actually, I’m not entirely sure what you’re wanting) hopefully this is similar enough to give you some ideas as to how to solve your problem.

It’s pretty object oriented so hopefully that’s not an issue. I’ve attached the .swf and a .zip with the various files included.

I haven’t been very diligent with the comments - though enough are there to get you through.

To use it - just click and drag a letter. It would probably be better if the momentum decayed as the collisions occured - this is a job for you - bed for me :asleep:

If you find this useful, it would be great if you would stop by my blog and leave a comment: http://www.footloosemoose.com/michael_wp

Cheers,

MB

i have been looking for some help on the same thing bobocop

battleman is close
basicly what he has minus the movment after they collide
i tried to alter what he has but there is so much extra stuff in there that i completey broke it :frowning:

Hi everyone and thanks, I’m still searching on this

battleman is close
basicly what he has minus the movment after they collide
i tried to alter what he has but there is so much extra stuff in there that i completey broke it
That Is close.
I can vew the .swf file but I’m getting an error with the .fla file when I tried to preview publish
Letter.as: Line 1: The name of this class, ‘Letter’, conflicts with the name of another class that was loaded, ‘Letter’.
class Letter extends MovieClip {

I’ll try and see If I can play around with It and let you know.

In the mean time I found this (see attach)
The movement Is what I need eccept there are only 3 objects and they did It different then I’m doing It.
I have all my letters layed out on stage as movie clips.
But they used a different method (arrays and a button).

It shouldn’t be hard to add more objects to the code, But I’m having a problem attaching that code to my movie clips.

check out the attached .fla file It might help you and If you figure it out let me know and I’ll do the same or If anyone can help with either one

By the way here Is my fla file so you can see what I’m trying to do.
right now only the H moves and when It hits the 3 It just stops but even so sometimes It stops overlapped and I then can’t move It again.

I can’t attach my fla file because It acceeds file size for this site.
But heres a pic of what I’m doing.
I will be adding more letters to It and Leaving about 3 or 4 empty spots to move around

Ooooh… right! Now I get it :slight_smile:

I’ll try and have a quick go tonight.

What’s the gap on the right for?

MB

Hi All,

I’ve had another go - I think you’ll find that this is closer to what you were after! Sorry I missed the mark with the last attempt - the screenshot really helped clarify it for me.

Check them out

Grid: 6 x 6 - 26 Tiles
http://img95.imageshack.us/my.php?image=alphabetmover26x626tiles2bt.swf

Grid: 10 x 10 - 26 Tiles
http://img95.imageshack.us/my.php?image=alphabetmover210x1026tiles4pq.swf

Grid: 10 x 10 - 80 Tiles
http://img95.imageshack.us/my.php?image=alphabetmover210x1080tiles2hy.swf

Grid: 20 x 20 - 26 Tiles
http://img95.imageshack.us/my.php?image=alphabetmover220x2026tiles2zv.swf

… and the grand finale
Grid: 30 x 30 - 800 Tiles
http://img95.imageshack.us/my.php?image=alphabetmover230x30800tiles4gs.swf

Hope you like it :slight_smile:

The code is a little more advanced - though if you understand recursion, you shouldn’t have any problems. The solution uses a 2D array which tracks which cells in the grid are currently vacant… and an array of Letter Tiles that can be moved or pushed into vacant cells.

If you like it or use it - please[U][/U] drop by my blog and leave a comment :slight_smile:
Blog: http://www.footloosemoose.com/michael_wp

Battleman

Battleman, That Is AMAZING.
Thats exactly what I was looking for.
I spent the last few days going through google searching for tuts and can’t find anything like this.

I’m going to go through It and try to see how you did It so I can learn.

I owe you buddy.

I have over 200 3d models that I made In Milkshape to use with 3Dgame studio.
If you ever need anything like that just let me know.

Again Thanks Buddy

Just remember me when you’re rich and famous. :wink:

Glad I could help.

Good luck with the project and let me know when you’re done so I can check it out.

Cheers mate.