New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LIS-FTWD-BrandyMcArthur#156 #8
Open
bdmcarthur
wants to merge
1
commit into
ironhack-dev-squad-127:master
Choose a base branch
from
bdmcarthur:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hey Brandy! Very good job. If you check your treasure, you can see it is blinking, this happens because every time you move it is waiting for the load event listener. The following is your code for the
The suggestions I have for you are:
Let me know if this was not clear enough and we can talk it through! |
Hi Sonia,
That all makes sense, thanks so much!
…On Fri, Aug 23, 2019 at 11:06 AM soniameller ***@***.***> wrote:
Hey Brandy! Very good job.
I will add some comments so that you get a bit further.
If you check your treasure, you can see it is blinking, this happens
because every time you move it is waiting for the load event listener.
The following is your code for the drawTreasure() function
function drawTreasure (){
if(treasureItem.row === player.row && treasureItem.col === player.col){
treasureItem.row = Math.floor(Math.random() * Math.floor(10));
treasureItem.col = Math.floor(Math.random() * Math.floor(10));
}
let position = [treasureItem.row * 50, treasureItem.col * 50]
const image = new Image();
image.src = 'images/treasure.png';
image.addEventListener('load', function () {
const imageWidth = image.width/4.5;
const imageHeight = image.height/4.5;
ctx.drawImage(image, position[0], position[1], imageWidth, imageHeight);
});
}
The suggestions I have for you are:
- For cleaner code, always define the variable in the top.
- In the example below I removed the definition of width and height
from the event and put them in the top as well.
- I added an if statement with the condition image.complete. This is a
property that returns whether or not the browser is finished loading an
image. This will prevent the blinking.
function drawTreasure() {
const image = new Image();
image.src = "images/treasure.png";
const imageWidth = image.width / 4.5;
const imageHeight = image.height / 4.5;
let position = [treasureItem.row * 50, treasureItem.col * 50];
if (image.complete) {
ctx.drawImage(image, position[0], position[1], imageWidth, imageHeight);
} else {
image.addEventListener('load',() => {
ctx.drawImage(image, position[0], position[1], imageWidth, imageHeight);
});
}
}
- There was also a problem when the player got the treasure. You are
checking and drawing in the function treasure so you call the random
function a bit late. If you move the condition to where you are listening
to key events it will update when you get into the cell.
- Remember is always better practice to eddEventListener(event,
function) than calling for example .onkeydown
document.addEventListener('keydown',function(e) {
e.preventDefault();
switch (e.keyCode) {
case 37:
player.moveLeft();
break;
case 38:
player.moveUp();
break;
case 39:
player.moveRight();
break;
case 40:
player.moveDown();
break;
}
if (treasureItem.row === player.row && treasureItem.col === player.col) {
treasureItem.row = Math.floor(Math.random() * Math.floor(10));
treasureItem.col = Math.floor(Math.random() * Math.floor(10));
}
drawEverything();
});
Let me know if this was not clear enough and we can talk it through!
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#8?email_source=notifications&email_token=AGHK2OJYJ3EK6PO4VCY6XXTQF6ZAVA5CNFSM4IOXLQK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD47YNDY#issuecomment-524256911>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGHK2OPESBLNKEI6EM4NOJTQF6ZAVANCNFSM4IOXLQKQ>
.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.