Answers is deprecated.

Please use Forum to participate in community by asking and answering questions!

I notice there are some questions with the same topic, but they all use the playcanvas editor.

I am using playcanvas engine without the editor, and here is my folder structure:

├── app.js
├── camera.js
├── index.html
└── libs
    └── playcanvas-stable.js

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Demo</title>
  <style>
   body {
       margin: 0;
       padding: 0;
   }
  </style>
  <script src="/libs/playcanvas-stable.js"></script>
</head>
<body>
  <canvas id="application"></canvas>
  <script src="/app.js"></script>
</body>
</html>

camera.js

pc.script.create('Camera', function (context) {
  var Camera = function (entity) {
    this.entity = entity;
  };

  Camera.prototype = {
    update: function (dt) {
      console.log(dt); // do not trigger.
    }
  };

  return Camera;
});

app.js

var canvas = document.getElementById('application');
var app = new pc.Application(canvas, {});
app.start();

app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);

var cameraEntity = new pc.Entity();
cameraEntity.addComponent('camera');
app.root.addChild(cameraEntity);
app.systems.script.addComponent(cameraEntity, {
  scripts: [{
    name: 'Camera',
    url: './camera.js'
  }]
}); // not work.

I found most posts mention to use context.systems.script.addComponent(entity, { scripts: [] }) to add script to entity programmatically, but I am failed.

Can anyone tell me how to do it ? thanks.

asked 27 Feb, 03:09

scarlex's gravatar image

scarlex
0224
accept rate: 100%

edited 27 Feb, 03:30

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×95

Asked: 27 Feb, 03:09

Seen: 358 times

Last updated: 27 Feb, 03:30

about | privacy | support | contact

playcanvas