Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何让旋转木马循环回到开头

如何让旋转木马循环回到开头
EN

Stack Overflow用户
提问于 2011-09-20 00:55:41
回答 2查看 1.2K关注 0票数 0

我有一个800瓦的电影剪辑包含4个面板彼此相邻,每个与200宽度(See picture here)。当我单击并拖动mouse_out时,它会检测到鼠标的方向,并根据方向将面板向左或向右移动200。

我的问题是我想让它循环,所以当它到达最左边的面板时,它会继续到左边,反之亦然,当它到达最右边的面板时,我应该能够继续点击并拖动运动。

我不确定这是不是一个旋转木马。

无论如何,这就是我到目前为止所拥有的。我在toLeftTween()toRightTween()上写了两条评论"What happens now“来指出我的问题所在。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import com.greensock.*;
import com.greensock.easing.*;

var selectX:Number = 0;
var mouseX1:int = 0;
var mouseX2:int = 0; 
var mcPosX:int = 0;

var contents:MovieClip = all_mc;
var draggable:Boolean = true;

contents.buttonMode = true;
contents.mouseChildren = false;
contents.x = 0;
contents.y = 70;

addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

function mouseDownHandler(e:MouseEvent):void {  
    //select the correct point on mc
    selectX = contents.x - mouseX;

    //for prediction direction later - mouse point 1
    mouseX1 = stage.mouseX;
    //trace("1: " + mouseX1);

    // move mc with mouse
    if (draggable) {
        addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
    } else {
        trace("unable to drag");
    }
}

function mouseUpHandler(e:MouseEvent):void {
    //for prediction direction later - mouse point 2
    mouseX2 = stage.mouseX;
    //trace("2: " + mouseX2);

    //remove mc move with mouse
    removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);    
    //check for direction of the mc based on mouseX1 and mouseX2
    mouseDirection();
}

function onEnterFrameHandler(e:Event):void {
    contents.x = parent.mouseX + selectX;
}

function mouseDirection():void {
    if (mouseX1 > mouseX2) {
        trace("to the left");
        toLeftTween();
    } else if (mouseX1 < mouseX2) {
        trace("to the right");
        toRightTween();
    } else {
        trace("nothing happened");
    }
}

function toLeftTween():void {
    if(contents.x<1 && contents.x>199) {
        mcPosX = 0;
        trace("to left - Panel 1");
    } else if(contents.x<-1 && contents.x>-199) {
        mcPosX = -200;
        trace("to left - Panel 2");
    } else if(contents.x<-201 && contents.x>-399) {
        mcPosX = -400;
        trace("to left - Panel 3");
    } else if(contents.x<-401 && contents.x>-599) {
        mcPosX = -600;
        trace("to left - Panel 4");
    } else if(contents.x>-600) {
        //What happens now?
    }
    var toLeftTween:TweenLite = new TweenLite(contents,0.25, {x:mcPosX});           
}

function toRightTween():void {
    if(contents.x<-601 && contents.x>-799) {
        mcPosX = -600;
        trace("to right - Panel 4");
    } else if(contents.x<-401 && contents.x>-599) {
        mcPosX = -400;
        trace("to right - Panel 3");
    } else if(contents.x<-201 && contents.x>-399) {
        mcPosX = -200;
        trace("to right - Panel 2");
    } else if(contents.x<-1 && contents.x>-199) {
        mcPosX = 0;
        trace("to right - Panel 1");
    } else if(contents.x<-2) {
        //What happens now?
    }
    var toRightTween:TweenLite = new TweenLite(contents,0.25, {x:mcPosX});
}
EN

回答 2

Stack Overflow用户

发布于 2011-09-20 03:02:33

干得好。当你看到它的时候,解决方案实际上非常简单。

您需要在每一帧中检查鼠标起点和当前鼠标点之间的距离,就像您使用selectXmouseX所做的那样。你的问题是,当你超过一定的距离时,你没有逻辑来处理你的循环。

因为它是一个循环,所以您需要检查起点和当前点之间的距离是否大于旋转木马影片剪辑的宽度,即800。如果是这样的话,您需要逻辑循环回自身。(但是,我不确定为什么toLeftTweentoRightTween函数会检查contents.x是否在负范围内……我想这是因为电影剪辑上的注册点?)

(编辑:我知道你做了什么-为什么你有负数。您可以根据需要将电影剪辑向左滑动。现在我来看一下您这样做的方式,模数实际上不会起作用,因为您检查的范围是-600到200之间,而不是0到800之间……)

你需要这样的东西。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function toLeftTween():void {
    // If contents.x is divisible by the width of the movieclip, it's greater than the mc width.
    // We check this here, and grab the remainder.

    if (contents.x > 800 || contents.x < -600)
        contents.x = contents.x % 800; // Modulus operator. Returns remainder.

    if(contents.x<1 && contents.x>199) {
        mcPosX = 0;
        trace("to left - Panel 1");
    } else if(contents.x<-1 && contents.x>-199) {
        mcPosX = -200;
        trace("to left - Panel 2");
    } else if(contents.x<-201 && contents.x>-399) {
        mcPosX = -400;
        trace("to left - Panel 3");
    } else if(contents.x<-401 && contents.x>-599) {
        mcPosX = -600;
        trace("to left - Panel 4");
    } else if(contents.x>-600) { // I think you flipped a sign here... -lunchmeat
        //What happens now?
        // Due to the modulus, we should never find ourselves in this situation!
    }
    var toLeftTween:TweenLite = new TweenLite(contents,0.25, {x:mcPosX});           
}

这或多或少应该能起到作用。您需要将其添加到两个补间函数中。(它可能需要稍微调整一下。)如果你遇到任何问题,请告诉我。祝好运!

票数 0
EN

Stack Overflow用户

发布于 2011-09-20 14:08:27

如果你想要这样的东西

(注意:numbers代表面板,索引0是初始点。)

对于索引0 1-2-3-4

对于索引1 2-3-4-1

对于索引-1 4-1-2-3

你好像走错路了。

首先,如果你需要继续循环,你必须把看不到的电影片段移到最后。你需要克隆项目末尾的第一个元素,以便继续查看。我更喜欢使用bitmapData的内容,所以你不需要克隆的第一个元素,它将计算更快,方法更容易。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7478986

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文