0
Question about javascript components...?
With this code... function startGame() { myGamePiece = new component(30, 30, "images/basketball.png", 10, 120, "image"); myBackground = new component(481, 271, "images/woodgrain.jpg", 0, 0, "background"); } ...how do I make the images accessible. I know they would need to be linked externally, but, how do I add the URL to this line of code?
7 Respuestas
+ 2
Yeah
It won't because it was set for color
Wait Let me send a code on how to add image.
+ 1
Explaining this might be too Long
See this Code
https://code.sololearn.com/WOp6WkMQ4vr1/?ref=app
+ 1
but it doesn’t add an image to mygamepiece? It’s just a color. i need to add an “external link” to the allow access to users on the web.
+ 1
I made an external js file images.js from gist for you to understand exactly how does class component works.
Check out the code here..
https://code.sololearn.com/W3l426R8h9NZ/?ref=app
and the external js file, https://gist.github.com/cvcodes/fadf035abf6d65c3748ff469a62bdecf
0
function component(width, height, color, x, y, type) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
if (type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
0
The Code above is how to add an image with the component
Make sure you set component as you did in your question
new component(30,30, "images/imgurl.jpg", 10, 120, "image") ;
0
I’ve been having “script errors” when trying project code on my phone using SoloLearn. Well, after about a week and a half of trying to troubleshoot...I discovered it was a simple fix🤦🏽♂️
I had to re-arrange some variables and functions in my JavaScript code. Some variables needed to be local to the functions, some global.🤷🏽♂️ Some functions won’t work global, only local😒 But, I got it working🙏🏾