我有一个计算机科学项目,我必须用P5 Play制作一台老虎机,但我一直收到这个错误:

SecurityError:从‘Window’读取命名属性‘Add’失败:阻止源为"https://preview.p5js.org""的帧访问跨源帧.

以下是我的代码:

//Load all images and create all of the groups/sprites
function preload() {
  a = loadImage("Images/Seven.jpg");
  b = loadImage("Images/bell.jpg");
  c = loadImage("Images/grape.jpg");
  d = loadImage("Images/onebar.jpg");
  e = loadImage("Images/threebar.jpg");
  f = loadImage("Images/twobar.jpg");

  top = new Group();
  bottom = new Group();
  middle = new Group();
  machine = new Sprite(0, 450, "static");
  machine1 = new Sprite(0, 50);

  machine.layer = 3;
  machine1.layer = 3;
  machine.visible = false;
  machine1.visible = false;
  machine1.collider = "none";

  
}

//Generate random sprites for the middle row
function ifListMid() {
  let randomize = Math.floor(random() * 6 + 1);

  if (randomize == 1) {
    bell = new Sprite();
    middle.add(bell);
    bell.addImage("normal", b);
    bell.scale = 0.03;
    bell.layer = 2;
  }

  if (randomize == 2) {
    grape = new Sprite();
    middle.add(grape);
    grape.addImage("normal", c);
    grape.scale = 0.03;
    grape.layer = 2;
  }

  if (randomize == 3) {
    seven = new Sprite();
    middle.add(seven);
    seven.addImage("normal", a);
    seven.scale = 0.03;
    seven.layer = 2;
  }

  if (randomize == 4) {
    onebar = new Sprite();
    middle.add(onebar);
    onebar.addImage("normal", d);
    onebar.scale = 0.04;
    onebar.layer = 2;
  }

  if (randomize == 5) {
    twobar = new Sprite();
    middle.add(twobar);
    twobar.addImage("normal", f);
    twobar.scale = 0.034;
    twobar.layer = 2;
  }

  if (randomize == 6) {
    threebar = new Sprite();
    middle.add(threebar);
    threebar.addImage("normal", e);
    threebar.scale = 0.03;
    threebar.layer = 2;
  }
}

//Generate random sprites for the top side
function ifListTop() {
  let randomize = Math.floor(random() * 6 + 1);

  if (randomize == 1) {
    bell = new Sprite();
    top.add(bell);
    bell.addImage("normal", b);
    bell.scale = 0.03;
    bell.layer = 2;
  }

  if (randomize == 2) {
    grape = new Sprite();
    top.add(grape);
    grape.addImage("normal", c);
    grape.scale = 0.03;
    grape.layer = 2;
  }

  if (randomize == 3) {
    seven = new Sprite();
    top.add(seven);
    seven.addImage("normal", a);
    seven.scale = 0.03;
    seven.layer = 2;
  }

  if (randomize == 4) {
    onebar = new Sprite();
    top.add(onebar);
    onebar.addImage("normal", d);
    onebar.scale = 0.04;
    onebar.layer = 2;
  }

  if (randomize == 5) {
    twobar = new Sprite();
    top.add(twobar);
    twobar.addImage("normal", f);
    twobar.scale = 0.034;
    twobar.layer = 2;
  }

  if (randomize == 6) {
    threebar = new Sprite();
    top.add(threebar);
    threebar.addImage("normal", e);
    threebar.scale = 0.03;
    threebar.layer = 2;
  }
}

//Generate random sprites for the bottom row
function ifListBottom() {
  let randomize = Math.floor(random() * 6 + 1);

  if (randomize == 1) {
    bell = new Sprite();
    bottom.add(bell);
    bell.addImage("normal", b);
    bell.scale = 0.03;
    bell.layer = 2;
  }

  if (randomize == 2) {
    grape = new Sprite();
    bottom.add(grape);
    grape.addImage("normal", c);
    grape.scale = 0.03;
    grape.layer = 2;
  }

  if (randomize == 3) {
    seven = new Sprite();
    bottom.add(seven);
    seven.addImage("normal", a);
    seven.scale = 0.03;
    seven.layer = 2;
  }

  if (randomize == 4) {
    onebar = new Sprite();
    bottom.add(onebar);
    onebar.addImage("normal", d);
    onebar.scale = 0.04;
    onebar.layer = 2;
  }

  if (randomize == 5) {
    twobar = new Sprite();
    bottom.add(twobar);
    twobar.addImage("normal", f);
    twobar.scale = 0.034;
    twobar.layer = 2;
  }

  if (randomize == 6) {
    threebar = new Sprite();
    bottom.add(threebar);
    threebar.addImage("normal", e);
    threebar.scale = 0.03;
    threebar.layer = 2;
  }
}

function setup() {
  //Create the canvas
  new Canvas(500, 500);

  //Set background color
  background(255, 255, 255);

  //Set frame rate
  setFrameRate = 60;

  machine.width = 1000;
  machine.height = 150;
  machine.color = "black";
  machine1.width = 1000;
  machine1.height = 150;
  machine1.color = "black";

  //Call all the function
  for (let i = 0; i < 3; i++) {
    ifListMid();
    ifListTop();
    ifListBottom();
  }

  //Set position to the middle row sprites
  middle[0].x = 125;
  middle[0].y = 250;

  middle[1].x = 250;
  middle[1].y = 250;

  middle[2].x = 375;
  middle[2].y = 250;

  //Set position to the top sprites
  top[0].x = 125;
  top[0].y = 160;

  top[1].x = 250;
  top[1].y = 160;

  top[2].x = 375;
  top[2].y = 160;

  //Set position for the bottom sprites
  bottom[0].x = 125;
  bottom[0].y = 340;

  bottom[1].x = 250;
  bottom[1].y = 340;

  bottom[2].x = 375;
  bottom[2].y = 340;
}

我已经为此努力了一个多小时,但我还是想不出办法,有人能帮我吗?

我试着在网上找到有关它的信息,但几乎没有解决我的问题的办法.

推荐答案

我只需要一行代码就可以重现您的问题.将问题分解到本质上对于调试很重要,这样您就可以深入了解导致问题的确切原因:

top.add;

此片段有助于表明它与p5或p5play无关.

请注意,如果我们将top更改为其他名称,如top_,问题就会消失.问题是window.top是一个预先存在的window(全局)属性,不允许访问属性.

除了重命名变量之外,您可以(也应该)始终使用let variableName;来声明变量.这可确保不会与全局window对象发生冲突,该对象上有许多无法touch 的常用名称.在P5项目中try 这样的设置:

let top;
let bottom;
let whateverOtherVarsYouNeed;

function setup() {
  top = foo();
  bottom = bar();
  whateverOtherVarsYouNeed = whatever();
}

function draw() {
  // use variables
}

所以,有两个要点:

  1. 总是尽量减少问题
  2. 不要使用全局变量

另请参见Error: Permission denied to access property "x".

Javascript相关问答推荐

fetch在本地设置相同来源的cookie,但部署时相同的代码不会设置cookie

获取表格的左滚动位置

如何使用侧边滚动按钮具体滚动每4个格?

useNavigation更改URL,但不呈现或显示组件

扫描qr code后出错whatter—web.js

判断表格单元格中是否存在文本框

使用原型判断对象是否为类的实例

为什么123[';toString';].long返回1?

用于在路径之间移动图像的查询

有条件重定向到移动子域

为什么JPG图像不能在VITE中导入以进行react ?

从另一个数组中的对应行/键值对更新数组中的键值对对象

MongoDB受困于太多的数据

传递方法VS泛型对象VS事件特定对象

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

如何在Java脚本中对数据进行签名,并在PHP中验证签名?

如何使用抽屉屏幕及其子屏幕/组件的上下文?

如何在脚本编译后直接将RxJ模块导入浏览器(无需Angel、webpack、LiteServer)

在JS/TS中构造RSA公钥

JavaScript -如何跳过某个字符(S)来打乱字符串中的字符