Question about javascript components...? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

7th Jul 2018, 4:58 PM
ThThinka
ThThinka - avatar
7 Answers
+ 2
Yeah It won't because it was set for color Wait Let me send a code on how to add image.
7th Jul 2018, 6:17 PM
Femi
Femi - avatar
+ 1
Explaining this might be too Long See this Code https://code.sololearn.com/WOp6WkMQ4vr1/?ref=app
7th Jul 2018, 5:16 PM
Femi
Femi - avatar
+ 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.
7th Jul 2018, 6:13 PM
ThThinka
ThThinka - avatar
+ 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
8th Jul 2018, 4:55 AM
Calviղ
Calviղ - avatar
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); } }
7th Jul 2018, 6:52 PM
Femi
Femi - avatar
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") ;
7th Jul 2018, 6:54 PM
Femi
Femi - avatar
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🙏🏾
25th Jul 2018, 8:38 AM
ThThinka
ThThinka - avatar