AS3:滑动和平移运动的碰撞问题

时间:2014-07-15 15:03:11

标签: actionscript-3 collision-detection

所以我目前在舞台上都有围墙。中间有一个正方形。用户当前能够在屏幕周围平移方块并滑动以移动方块。我遇到的问题是,当用户进行平移时,方块不会停在墙上,它会稍微超过它但会停在某个位置。但是,当用户进行滑动动作时,它只是继续前进而忽略了墙壁就在那里..所以这是我正在使用的代码。

import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.events.TransformGestureEvent;
import flash.events.MouseEvent;
import flash.display.Shape;
import flash.events.TouchEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Point;

stage.frameRate = 60;

Multitouch.inputMode = MultitouchInputMode.GESTURE;

var myshape:Shape;
myshape = new Shape();
myshape.graphics.lineStyle(8, Math.random() * 0xFFFFFF);
myshape.filters = [new BlurFilter()];

var friction:Number = .85;
square_mc.spd = 2;

addEventListener(Event.ENTER_FRAME, main);
square_mc.addEventListener(TransformGestureEvent.GESTURE_PAN, onPan);
square_mc.addEventListener(TransformGestureEvent.GESTURE_ROTATE, onRotate);
square_mc.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe);
Square_btn.addEventListener(MouseEvent.CLICK, addSquare);
stage.addEventListener(MouseEvent.MOUSE_DOWN, activateDraw);

MovieClip(root).square_mc.visible = false;

function main(e:Event):void{
    square_mc.prevX = square_mc.x;
    square_mc.prevY = square_mc.y;

    square_mc.vx *= friction;
    square_mc.vy *= friction;

    square_mc.x += square_mc.vx;
    square_mc.y += square_mc.vy;

    if(collision(square_mc.cp, level_mc)){
        square_mc.x = square_mc.prevX;
        square_mc.y = square_mc.prevY;
    }
}

function collision(_cp:Array, _obj:MovieClip):Boolean{
    for(var i:int = 0; i < _cp.length; i++){
        if(_obj.hitTestPoint(_cp[i].x, _cp[i].y, true)){
            return true;
        }
    }
    return false;
}

function activateDraw(event:MouseEvent):void{
    myshape.graphics.moveTo(mouseX, mouseY);
    addChild(myshape);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
}

function lineDraw(event:MouseEvent):void{
    myshape.graphics.lineTo(mouseX, mouseY);
    event.updateAfterEvent();
}

function stopDraw(event:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    stage.removeEventListener(MouseEvent.MOUSE_UP, stopDraw);

    myshape.graphics.clear();
    myshape.graphics.lineStyle(8, Math.random() * 0xFFFFFF);
}

stopDraw(null);

function onPan (e:TransformGestureEvent):void{
    square_mc.y += e.offsetY;
    square_mc.x += e.offsetX;
    square_mc.gotoAndStop(3);
}

function onRotate (e:TransformGestureEvent):void{
    square_mc.rotation += e.rotation;
    square_mc.gotoAndStop(2);
}

function onZoom (e:TransformGestureEvent):void{
    square_mc.scaleX *= e.scaleX;
    square_mc.scaleY *= e.scaleY;
    square_mc.gotoAndStop(4);
}


function onSwipe (e:TransformGestureEvent):void{
    if (e.offsetX == 1) { 
        //User swiped towards right
        square_mc.x += 100; 
    }
    if (e.offsetX == -1) { 
        //User swiped towards left
        square_mc.x -= 100;
    } 
    if (e.offsetY == 1) { 
        //User swiped towards bottom
        square_mc.y += 100; 
    }
    if (e.offsetY == -1) { 
        //User swiped towards top
        square_mc.y -= 100;
    } 
}

function addSquare (e:MouseEvent):void{
    if(Square_btn.currentFrame <= 1){
        Square_btn.gotoAndStop(2);
        MovieClip(root).square_mc.visible = true;
    }else{
        Square_btn.gotoAndStop(1);
        square_mc.gotoAndStop(1);
        MovieClip(root).square_mc.visible = false;
}
}

0 个答案:

没有答案