Simple Flash Snap-To-Grid (Flash AS 2.0)

This post has been moved.

Advertisements

9 Responses

  1. Hi! This what I got , when I tried to run your program:

    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
    var ball:MovieClip = this.createEmptyMovieClip(”ball”, this.getNextHighestDepth());

    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: ‘)’ or ‘,’ expected
    ball.lineStyle(70, 0×0, 100);

    Total ActionScript Errors: 2 Reported Errors: 2

  2. Here is the complete script without errors:

    var size:Number = 100;
    var columns:Number = Math.ceil(Stage.width/size);
    var rows:Number = Math.ceil(Stage.height/size);
    var ball_move:Boolean = false;
    var ball:MovieClip = this.createEmptyMovieClip(“ball”, this.getNextHighestDepth());
    ball.lineStyle(70, 0x000000, 100);
    ball.lineTo(0, 1);
    ball._x = 200;
    ball._y = 200;
    ball.onPress = function() {
    this.startDrag();
    ball_move = true;
    };
    ball.onMouseUp = function() {
    this.stopDrag();
    ball_move = false;
    };
    ball.onMouseMove = function() {
    if (!ball_move) {
    return;
    }
    this._x = _xmouse-_xmouse%size;
    this._y = _ymouse-_ymouse%size;
    updateAfterEvent();
    };
    function drawSquare(who:String, to_x, to_y) {
    this.createEmptyMovieClip(who, this.getNextHighestDepth());
    this[who].lineStyle(1, 0x000000, 100);
    this[who].moveTo(0, 0);
    this[who].lineTo(size, 0);
    this[who].lineTo(size, size);
    this[who].lineTo(0, size);
    this[who]._x = to_x;
    this[who]._y = to_y;
    }
    for (var c = 0; c

  3. Sry the one above was not complete, and remember to erase the doublequotes in flash and re-write them yourself. For some reason wordpress formats the text wrong :(

    var size:Number = 100;
    var columns:Number = Math.ceil(Stage.width/size);
    var rows:Number = Math.ceil(Stage.height/size);
    var ball_move:Boolean = false;
    var ball:MovieClip = this.createEmptyMovieClip(“ball”, this.getNextHighestDepth());
    ball.lineStyle(70, 0x000000, 100);
    ball.lineTo(0, 1);
    ball._x = 200;
    ball._y = 200;
    ball.onPress = function() {
    this.startDrag();
    ball_move = true;
    };
    ball.onMouseUp = function() {
    this.stopDrag();
    ball_move = false;
    };
    ball.onMouseMove = function() {
    if (!ball_move) {
    return;
    }
    this._x = _xmouse-_xmouse%size;
    this._y = _ymouse-_ymouse%size;
    updateAfterEvent();
    };
    function drawSquare(who:String, to_x, to_y) {
    this.createEmptyMovieClip(who, this.getNextHighestDepth());
    this[who].lineStyle(1, 0x000000, 100);
    this[who].moveTo(0, 0);
    this[who].lineTo(size, 0);
    this[who].lineTo(size, size);
    this[who].lineTo(0, size);
    this[who]._x = to_x;
    this[who]._y = to_y;
    }
    for (var c = 0; c

  4. GOD DAMNIT!! WORD PRESS CUTS OFF MY COMMENTS!!! HERE IS THE LAST PART!!!!

    continues on same line as last piece of code from above:

  5. nice tut, I had couple of errors, but got rid of them works fine now.

    Errors:
    1.lineStyle(1,0x0000ff,100): x in the rgb hexadecimal
    part has to be erased and rewritten.
    2.The double quotes have to be replaced.

    As well, the above code has ball clip created first.
    The
    createEmptyMovieClip(“ball”,this.getNextHighestDepth())
    along with it’s mouse handlers have to be placed after the for loop that generates the grid.
    Currently your code generates the grid and places circle behind it, this is due to getNextHighestDepth.

  6. faisal, Thanks for the feedback. We’ll try and get the bug fixes in the next version of this post. Thanks for reading.

  7. Thanks, this was helpful. I think the problem with the copying of the code into Flash stems from the punctuation coming through as actual punctuation and not standard hash marks. I had to do a find replace on all the quotes and semi-colons.

    Ben

  8. Hey, thanks for this code. The _xmouse%size part is real usefull, though I still don’t really get how it works.

    By the way; I think this part:

    this._x = _xmouse – _xmouse%size;

    can be improved by adding half the gridsize to it:

    this._x = _xmouse – _xmouse%size + size/2;

    This makes the snapping feel a little better. Instead of having to move a hundered pixels to the right and having to move just 1 pixel to the left to make the ball move, you have to move it 50 pixels in either way to make it move if you add half the gridsize.

    Same goes for the y ofcourse

  9. Its me again.

    I was wondering why the ball was shaking when I moved the mouse, but not far enough to make the ball move.
    When I looked at this part:

    ball.onPress = function () {
    this.startDrag();
    ball_move = true;
    }

    I got it. Why are you using this.startDrag?
    You are setting the var ‘ball_move’ to ‘true’. And in the
    ball.onMouseMove you are checking for that var to see if it’s true. If so, the ball is allowed to move.
    So why use startDrag?
    You can leave that part out

    ball.onPress = function () {
    ball_move = true;
    }

    And it will move more fluidly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: