开发者

Flash CS5 startDrag only able to drag an object from a specific point

开发者 https://www.devze.com 2023-03-26 07:58 出处:网络
I have a pause screen that is being added when you click the pause button inside my current game. Once this is called, the PausePanel class kicks in and adds a \'view\' to itself to the stage (which i

I have a pause screen that is being added when you click the pause button inside my current game. Once this is called, the PausePanel class kicks in and adds a 'view' to itself to the stage (which is a single movie clip called PauseScreenView that contains all of the buttons, sliders, etc. that I need it to). In this pause panel, I'm trying to set up a custom slider so I can control the volume. Right now I'm ignoring the whole volume thing and just trying to get it to display some text in a dynamic text window on the same pause panel. Now I have everything set up and working, however, there is a problem. I can only "grab" the slider knob (called volKnob) when it is at the far ends of it's ra开发者_开发问答nge. And to make things worse, I can't grab it from anywhere on the knob movieclip graphic except the left edge when it's on the left side of its range and the right edge when it's on the right side of its range. Also if I let go of it while not at a far edge, then I can't grab it again at all. I've set up other drags before, and one is even in another place of this game. They all work fine and I can grab the object from anywhere I want within the edges of the graphic. But on this page I can't seem to get it except for the edges. Now this is the first time I have tried setting a limit to it's range, but if I take that bit out it still won't let me grab it from just anywhere. Here is the PausePanel.as code:

package {
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

public class PausePanel extends MovieClip {
    public var viewPause:PauseScreenView;

    private var boundsRect:Rectangle;

    public function PausePanel():void {
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }

    private function addedToStage(e:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

        //add the graphics
        viewPause = addChild(new PauseScreenView()) as PauseScreenView;

        //set up variables
        boundsRect = new Rectangle(60, 185, 360, 0);

        addEventListener(Event.ENTER_FRAME, onEnterFrame);

        viewPause.Knob.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
    }

    private function onEnterFrame(event:Event):void {
        sliderValue = viewPause.Knob.x / 3;
        viewPause.status_txt.text = "Slider position is: " + sliderValue;
    }

    private function startDragging(event:MouseEvent):void {
        var currentDragObject:MovieClip = event.currentTarget as MovieClip;
        currentDragObject.startDrag(true, boundsRect);
            currentDragObject.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
    }

    private function stopDragging(event:MouseEvent):void {
        var currentDragObject:MovieClip = event.currentTarget as MovieClip;
         currentDragObject.stopDrag();
        currentDragObject.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
    }
}

}

Is there something that anyone see's where I missed something or set up something incorrectly? I have tried a number of different things and all of them have worked in the same messed up way. Any ideas?

UPDATED:

OK I took this code out and made it the constructor class for a blank fla with just a knob and text window. Once I took out the viewPause section of this, it works perfectly. It drags just like you think it would. The problem comes into being with the fact that the knob is a child of viewPause it seems? Is there some special trick to getting a drag to work when the drag object is a sub-object of another besides how I show it in the code (i.e. viewPause.Knob)??


Try to remove the startDragging event when it fires and then adding it again in the stopDragging event. Something like this:

private function startDragging(event:MouseEvent):void {
    currentDragObject.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);

    var currentDragObject:MovieClip = event.currentTarget as MovieClip;
    currentDragObject.startDrag(true, boundsRect);
        currentDragObject.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}

private function stopDragging(event:MouseEvent):void {
    var currentDragObject:MovieClip = event.currentTarget as MovieClip;
     currentDragObject.stopDrag();
    currentDragObject.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);

    currentDragObject.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
}

Also you may try to check this tutorial, it's easily adaptable to your problem. I used this code for a one-dimensional slider like the one you need: http://www.flashandmath.com/basic/dragdroptour/dd_tour2.html


This code actually works perfectly for anyone who happens to search this topic. My problem was occurring because I had a range set up that the mouse would not work so false clicks wouldn't occur on the screen that is below this one. The range just happened to be over top of the majority of this slider object I was creating except at the edges of the sliders range. Hence my problem. So in the end it was just programer error that caused this not to function properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号