简体   繁体   English

如何修复createjs中未在画布上显示的预加载资产?

[英]How to fix preloaded assets not showing on canvas in createjs?

I'm trying to create an animation by setting a background to a canvas and then trying to move my assets over the background image.我正在尝试通过为画布设置背景然后尝试将我的资产移动到背景图像上来创建动画。 I'm able to display the background image in the canvas and also able to preload all my assets.我能够在画布中显示背景图像,也能够预加载我的所有资产。 However, I'm not able to display any of the assets on the canvas.但是,我无法在画布上显示任何资产。

I tried to play around with the coordinates of the bitmap I'm trying to display in case if it's displaying outside the visible area.我试图玩弄我试图显示的位图的坐标,以防它显示在可见区域之外。 I also tried to check if there is any problem with the timing of updating the stage, but no luck.我还尝试检查更新阶段的时间是否有任何问题,但没有运气。 Any help would be appreciated.任何帮助,将不胜感激。

function set()
{
    var cgroup = new createjs.Container();

    cgroup.x = 100;
    cgroup.y = 100;


    // var path = 'img/cloud1.png';
    var img = preload.getResult("c1");
    console.log(img.src);  //displaying correct preloaded file
    var cloud1 = new createjs.Bitmap(img);
    cgroup.addChild(cloud1);
    cloud1.x = 120;
    cloud1.y = 120;
    cloud1.regX = 62;
    cloud1.regY = 62;

    stage.addChild(cgroup);
    stage.update();
}

You need to call the function to make it run.您需要调用该函数以使其运行。 For instance, this would be for when the page loads.例如,这将用于页面加载时。

window.onload() = function(){
  var cgroup = new createjs.Container();

  cgroup.x = 100;
  cgroup.y = 100;

  // var path = 'img/cloud1.png';
  var img = preload.getResult("c1");
  console.log(img.src); //displaying correct preloaded file
  var cloud1 = new createjs.Bitmap(img);
  cgroup.addChild(cloud1);
  cloud1.x = 120;
  cloud1.y = 120;
  cloud1.regX = 62;
  cloud1.regY = 62;

  stage.addChild(cgroup);
  stage.update();
};

原来我使用的是上下文,背景图像是使用 drawImage 显示的,然后当我向舞台添加另一个图像时,画布只显示第二张图像的舞台,因此清除了第一张图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM