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

Setting boundries for "pressmove".

Explorer ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

I want to set the x boundry of an object so it stops dragging between set values. For example, you can drag the object between 50px and 500px.

What code can I add to the following to achieve this?

var root = this;
var Cucumber = root.Cucumber;
createjs.Touch.enable(stage);

Cucumber.on("mousedown", function (e) {
e.currentTarget.offsetX = stage.mouseX / stage.scaleX - e.currentTarget.x;
});

Cucumber.on("pressmove", function (e) {
e.currentTarget.x = stage.mouseX / stage.scaleX - e.currentTarget.offsetX;
});

Views

129

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

correct answers 1 Correct answer

Engaged , Dec 22, 2022 Dec 22, 2022
var root = this;
var Cucumber = root.Cucumber;
createjs.Touch.enable(stage);

var MIN = 50;
var MAX = 500;

Cucumber.on("mousedown", function (e) {
    e.currentTarget.offsetX = stage.mouseX / stage.scaleX - e.currentTarget.x;
});

Cucumber.on("pressmove", function (e) {
    var xPos = stage.mouseX / stage.scaleX - e.currentTarget.offsetX;
    e.currentTarget.x = clamp(xPos, MIN, MAX);
});

function clamp(num, min, max) {
    return Math.min(Math.max(num, min), max);
}

 

 

Votes

Translate

Translate
Engaged ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

var root = this;
var Cucumber = root.Cucumber;
createjs.Touch.enable(stage);

var MIN = 50;
var MAX = 500;

Cucumber.on("mousedown", function (e) {
    e.currentTarget.offsetX = stage.mouseX / stage.scaleX - e.currentTarget.x;
});

Cucumber.on("pressmove", function (e) {
    var xPos = stage.mouseX / stage.scaleX - e.currentTarget.offsetX;
    e.currentTarget.x = clamp(xPos, MIN, MAX);
});

function clamp(num, min, max) {
    return Math.min(Math.max(num, min), max);
}

 

 

- Vlad: UX and graphic design, Flash user since 1998
Member of Flanimate Power Tools team - extensions for character animation

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
Explorer ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

LATEST

Thanks, that worked perfectly. I'd never heard of "clamp" and after researching it - it's perfect for this code. Thanks for teaching me something new.

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