gpt4 book ai didi

javascript - Uncaught ReferenceError : loadImage is not defined

转载 作者:行者123 更新时间:2023-11-30 21:07:49 25 4
gpt4 key购买 nike

我正在尝试从我的浏览器 (Chrome) 运行这个用 Javascript 编写的简单游戏:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="p5.js"></script>
<script src="CC Game Alessa Endspiel.js"></script>


</head>
<body>

</body>
</html>

这是我的 javascript 文件,其中包含 loadimage 代码。这些文件都在与 js 相同的文件夹中。和索引文件。

var gamestate = "intro";
//Früchte
var Strawberry = loadImage("Strawberry.png");
var Banana = loadImage("Banana.png");
var Coconut = loadImage("Coconut.png");
var Grapes = loadImage("Grapes.png");
var Pineapple = loadImage("Pineapple.png");
var Mango = loadImage("Mango.png");
var Raspberry = loadImage("Raspberry.png");
var Background1 = loadImage("Background1.jpg");
var Baba2 = loadImage("Baba2.png");
var StartScreen = loadImage("StartScreen.jpg");
var StartButton = loadImage("StartButton.png");
var TimeClock = 520;
var Princess = loadImage("indianprincess.jpg");

var player_img = image(Baba2,mouseX,470,19,19);

我完全是初学者,希望这不是一个太愚蠢的问题>。<

编辑:非常感谢!

现在我添加了预加载功能:

var Strawberry;
var Banana;
var Coconut;
var Grapes;
var Pineapple;
var Mango;
var Raspberry;
var Background1;
var Baba2;
var StartScreen;
var StartButton;
var TimeClock;
var Princess;

var player_img;


function preload() {

Strawberry = loadImage("Strawberry.png");
Banana = loadImage("Banana.png");
Coconut = loadImage("Coconut.png");
Grapes = loadImage("Grapes.png");
Pineapple = loadImage("Pineapple.png");
Mango = loadImage("Mango.png");
Raspberry = loadImage("Raspberry.png");
Background1 = loadImage("Background1.jpg");
Baba2 = loadImage("Baba2.png");
StartScreen = loadImage("StartScreen.jpg");
StartButton = loadImage("StartButton.png");
Princess = loadImage("indianprincess.jpg");

}
function setup() {
image(Strawberry, 0, 0);
image(Banana, 0, 0);
image(Coconut, 0, 0);
image(Grapes, 0, 0);
image(Pineapple, 0, 0);
image(Mango, 0, 0);
image(Raspberry, 0, 0);
image(Background1, 0, 0);
image(Baba2, 0, 0);
image(StartScreen, 0, 0);
image(StartButton, 0, 0);
image(Princess, 0, 0);
player_img = image(Baba2,mouseX,470,19,19);
}

现在有:未捕获的 ReferenceError:未定义 noStroke

现在我得到:

从来源“null”访问位于“file:///C:/Users/Uni/Desktop/CC%20Game%20Alessa%20Endspiel/Banana.png”的图像已被 CORS 策略阻止:响应无效。因此不允许访问 Origin 'null'。

-.-

最佳答案

根据文档 https://p5js.org/reference/#/p5/loadImage

loadImage 需要在 preload() 函数中调用

The image may not be immediately available for rendering If you want to ensure that the image is ready before doing anything with it, place the loadImage() call in preload(). You may also supply a callback function to handle the image when it's ready.

你能试试吗...

关于javascript - Uncaught ReferenceError : loadImage is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46445999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com