• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

I want to change the frame of the MovieClip by dragging the ball, but it's not working.

Community Beginner ,
Jun 01, 2023 Jun 01, 2023

Copy link to clipboard

Copied

My screen width is 800.
In the onPressMove function, I first set the range of ball's movement on the X-axis between 20 and 780.
I want to change the frame of car to 0 when the ball's position is { top: 1, right: 20, bottom: 1, left: 685.5 }, and change it to 1 when the ball's position is { top: 1, right: 114.5, bottom: 1, left: 590.15 }.

My ball can move now, but car not change the frame when ball move to the specific region.


Could anyone tell me what's wrong?


27546553d8ak_1-1685602929372.png
car have a lot of frame inside.

27546553d8ak_0-1685602905595.png

 

var root = this;
var ball = root.ball;
var car = root.car;

function main()
{
	createjs.Touch.enable(stage);
	stage.mouseMoveOutside = true;
	
	root.stop();

    ball.hitFrame = 1;
    ball.missFrame = 0;
    //ball.dropArea = { top: 1, right: 20, bottom: 1, left:  20 };
    ball.stopCar0 = { top: 1, right: 20, bottom: 1, left:  685.5 };
    ball.stopCar1 = { top: 1, right: 114.5, bottom: 1, left:  590.15 };
    ball.stopCar2 = { top: 1, right: 209.85, bottom: 1, left:  495.35 };
    ball.on("mousedown", onMouseDown);
}

function onMouseDown(e)
{
	var target = e.currentTarget;
	
	target.offset = { x: stage.mouseX / stage.scaleX - target.x, y: stage.mouseY / stage.scaleY - target.y };
	target.pressMoveListener = ball.on("pressmove", onPressMove);
	target.pressUpListener = ball.on("pressup", onPressUp);
}

function onPressMove(e)
{
	var target = e.currentTarget;
	
	target.x = stage.mouseX / stage.scaleX - target.offset.x;

    if (target.x < 20) {
        target.x = 20;
    } else if (target.x > 780) {
        target.x = 780;
    }
    else if (target.x > ball.stopCar0.left && target.x < ball.stopCar0.right)
	{
		root.car.gotoAndStop(0);
    }
    else if (target.x > ball.stopCar1.left && target.x < ball.stopCar1.right)
	{
		root.car.gotoAndStop(1);
    }
    else if (target.x > ball.stopCar2.left && target.x < ball.stopCar2.right)
	{
		root.car.gotoAndStop(2);
    }
}

function onPressUp(e)
{
	var target = e.currentTarget;

	target.off("pressmove", target.pressMoveListener);
	target.off("pressup", target.pressUpListener);
}

if (!root.frame0Started)
{
	main();
	root.frame0Started = true;
}

 

 

Views

81

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2023 Jun 01, 2023

Copy link to clipboard

Copied

Hi.

 

You need to swap the left and right properties in the stopCar1, stopCar2 and stopCar3 objects.

 

And you also need to check from the smallest region to the biggest. Meaning that you have to invert the else-if chain.

 

Like this:

var root = this;
var ball = root.ball;
var car = root.car;

function main()
{
	createjs.Touch.enable(stage);
	stage.mouseMoveOutside = true;
	
	root.stop();

    ball.stopCar0 = { top: 1, left: 20, bottom: 1, right:  685.5 };
    ball.stopCar1 = { top: 1, left: 114.5, bottom: 1, right:  590.15 };
    ball.stopCar2 = { top: 1, left: 209.85, bottom: 1, right:  495.35 };
    ball.on("mousedown", onMouseDown);
}

function onMouseDown(e)
{
	var target = e.currentTarget;
	
	target.offset = { x: stage.mouseX / stage.scaleX - target.x, y: stage.mouseY / stage.scaleY - target.y };
	target.pressMoveListener = ball.on("pressmove", onPressMove);
	target.pressUpListener = ball.on("pressup", onPressUp);
}

function onPressMove(e)
{
	var target = e.currentTarget;
	
	target.x = stage.mouseX / stage.scaleX - target.offset.x;

    if (target.x < 20)
	{
        target.x = 20;
    }
	else if (target.x > 780)
	{
        target.x = 780;
    }

	if (target.x > ball.stopCar2.left && target.x < ball.stopCar2.right)
	{
		root.car.gotoAndStop(2);
    }
	else if (target.x > ball.stopCar1.left && target.x < ball.stopCar1.right)
	{
		root.car.gotoAndStop(1);
    }
    else if (target.x > ball.stopCar0.left && target.x < ball.stopCar0.right)
	{
		root.car.gotoAndStop(0);
    }
}

function onPressUp(e)
{
	var target = e.currentTarget;

	target.off("pressmove", target.pressMoveListener);
	target.off("pressup", target.pressUpListener);
}

if (!root.frame0Started)
{
	main();
	root.frame0Started = true;
}

 

I hope this helps.

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 04, 2023 Jun 04, 2023

Copy link to clipboard

Copied

LATEST

Thank you for your response and the great suggestion. I have made the correction to my error. However, unfortunately, the image still does not change with the movement of the bar.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines