0

I am a complete novice in CreateJS and I am using it to try and make a simple canvas game.

The user clicks a point on the screen and the sprite moves and stops. I have different display containers in this order (Stage children: path mesh, background, sprite container). Ideally the user clicks anywhere on the canvas and the sprite will move as far as possible until it reaches the edge of the path mesh png.

I just can't figure out how to achieve this. It would be great if someone could point me in the right direction to approach this problem. Many thanks.

Path Mesh png

Current snippet of code used.

client.walkmesh.addEventListener("click", function(evt) {
    let pc = player.container
        
    let timeline = new createjs.Timeline();
    timeline.addTween(
        // Bounce
        createjs.Tween.get(playerChild, {loop:true})
        .wait(1).to({
            y:0,
        }).wait(1).to({
            y: -4,
        }).wait(1).to({
            y: -6,
        }).wait(1).to({
            y:  -4,
        }).wait(1).to({
            y: 0,
        }),
        // Move
        createjs.Tween.get(pc).to({x:evt.localX, y:evt.localY}, 
        calculateSpeed(evt.localX, evt.localY, pc.x, pc.y), 
        createjs.Ease.linear)
    );
});
2
  • Please update the question with the code you already have. Commented Jul 22, 2020 at 14:20
  • Updated the question to provide my current code I have tried. Commented Jul 22, 2020 at 15:21

1 Answer 1

0

If you are new at this, as you say, you could get the help of a framework. For instance, ZIM at https://zimjs.com has a MotionController() where you pass in the object you want to move, and the type of motion - for instance moving to a press, or following the mouse, or a key or gamepad. Then you can add a boundary as well which would make what you are trying to do quite simple.

var sprite = new Sprite().center();
var boundary = new Boundary(mesh.x, mesh.y, mesh.width-sprite.width, mesh.height-sprite.height);
new MotionController({target:sprite, boundary:boundary});

Otherwise, you can code it manually in a Ticker function. Move the x and y towards the mouse location. If the x and y is past the side of the bounds then set it to the bound. Basically, that is what the MotionController class is doing for you. Don't do it with Tween. And because you are not using a tween, you might then want to add damping... again, the MotionController does that for you. Otherwise, ZIM also has a Damp class to do the damping calculation for you.

If you need help - you are welcome to join us at https://zimjs.com/slack - cheers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.