davidfig / pixi-viewport

A highly configurable viewport/2D camera designed to work with pixi.js
https://davidfig.github.io/pixi-viewport/
MIT License
1.04k stars 174 forks source link

Viewport coordinates do not exist, viewport can't follow #2

Closed defaultsamson closed 6 years ago

defaultsamson commented 6 years ago

I can't tell if I'm using the plugin incorrectly, or if there's a bug happening.

I'm using pixi-viewport 0.6.1.

  1. When I try to get the center coordinates of a viewport, it returns {x: NaN, y: NaN}
  2. When I try to follow a target the camera will stay in one spot and prevent me from dragging it.

Here's the general outline of what I'm doing, does anything significantly stand out?

var game = new PIXI.Application(600, 600, {
    antialias: false,
    transparent: false,
    resolution: 1
});

const Viewport = require('pixi-viewport');

var viewport = new Viewport(game.stage);
viewport
    .drag()
    .pinch()
    .hitArea()
    .decelerate()
    .bounce()
    .start();

var bunny = new PIXI.Sprite(resources.bunny.texture);

bunny.x = game.renderer.width / 2;
bunny.y = game.renderer.height / 2;

var target = game.stage.addChild(bunny);

game.ticker.add(function () {
    bunny.x += 5

    console.log('x: ' + viewport.center.x + ' y: ' + viewport.center.y)
});

viewport.follow(bunny);
defaultsamson commented 6 years ago

Resolved. I did not properly pass in values for

[options.screenWidth] these values are needed for clamp, bounce, and pinch plugins
[options.screenHeight]
[options.worldWidth]
[options.worldHeight]

and was trying to use the pinch() and bounce() plugins.