英文:
If statement with rendering illusions before/after depending on position relative to set y point not working
问题
以下是已经翻译好的部分:
"To be more clear this block started as a code for solely mainchar and Realchair so that when my character is above a certain y level it will give the illusion that he is behind the chair by rendering the character before the chair and if my character is under the set y level then he is rendered after the chair to give the illusion that he is in front of the chair."
"ok but as you can see this is a switch because i have ImageTextureSide (mainchar facing right) and ImageTextureSideTwo (mainchar facing left), so i need to put this idea of the rendering illusion in both of the cases."
"Now here is where the problem arises. I have multiple furniture assets in my project so i figured i would add the idea of the rendering illusion to Rightchair, but for some reason when i added the same logic, everything worked perfectly fine except for when mainchar.y < the set y value. In the case where it is less than the set y value it should be rendered before the chair which is what is written in the if statement for that situation. But only when i added the logic for the right chair is when mainchar.y < the set y value it renders the mainchar after the chair."
英文:
To be more clear this block started as a code for solely mainchar and Realchair so that when my character is above a certain y level it will give the illusion that he is behind the chair by rendering the character before the chair and if my character is under the set y level then he is rendered after the chair to give the illusion that he is in front of the chair.
ok but as you can see this is a switch because i have ImageTextureSide (mainchar facing right) and ImageTextureSideTwo (mainchar facing left), so i need to put this idea of the rendering illusion in both of the cases.
Now here is where the problem arises. I have multiple furniture assets in my project so i figured i would add the idea of the rendering illusion to Rightchair, but for some reason when i added the same logic, everything worked perfectly fine except for when mainchar.y < the set y value. In the case where it is less than the set y value it should be rendered before the chair which is what is written in the if statement for that situation. But only when i added the logic for the right chair is when mainchar.y < the set y value it renders the mainchar after the chair.
I added an image where you can see mainchar rendered after the chair despite the if statement running?
switch (currentDirection) {
case SideLeft:
SDL_RenderCopy(renderer, imageTextureglassybig, nullptr, &Glassy);
if (mainchar.y > 250) {
// Main character is above the chair, render chair first
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
}
else {
// Main character is below the chair, render main character first
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
}
if (mainchar.y > 250) {
// Main character is above the chair, render chair first
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
}
else {
// Main character is below the chair, render main character first
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
}
SDL_RenderCopy(renderer, imageTextureTChair, nullptr, &Topchair);
SDL_RenderCopy(renderer, imageTextureTChair3, nullptr, &Topchair3);
SDL_RenderCopy(renderer, imageTexturetable, nullptr, &Bigtable);
SDL_RenderCopy(renderer, imageTextureBChair, nullptr, &Bottomchair);
SDL_RenderCopy(renderer, imageTextureBChair2, nullptr, &Bottomchair2);
SDL_RenderCopy(renderer, imageTextureRminitable, nullptr, &Minitbl);
break;
case SideRight:
SDL_RenderCopy(renderer, imageTextureglassybig, nullptr, &Glassy);
if (mainchar.y < 250) {
// Main character is above the chair, render chair first
SDL_RenderCopy(renderer, imageTextureSide, nullptr, &mainchar);
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
cout << ("\n RIGHTif");
}
else {
// Main character is below the chair, render main character first
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
SDL_RenderCopy(renderer, imageTextureSide, nullptr, &mainchar);
cout << ("RIGHTelse");
}
if (mainchar.y < 250) {
// Main character is above the chair, render chair first
SDL_RenderCopy(renderer, imageTextureSide, nullptr, &mainchar);
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
cout << ("\n RIGHTif");
}
else {
// Main character is below the chair, render main character first
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
SDL_RenderCopy(renderer, imageTextureSide, nullptr, &mainchar);
cout << ("RIGHTelse");
}
SDL_RenderCopy(renderer, imageTextureTChair, nullptr, &Topchair);
SDL_RenderCopy(renderer, imageTextureTChair3, nullptr, &Topchair3);
SDL_RenderCopy(renderer, imageTexturetable, nullptr, &Bigtable);
SDL_RenderCopy(renderer, imageTextureBChair, nullptr, &Bottomchair);
SDL_RenderCopy(renderer, imageTextureBChair2, nullptr, &Bottomchair2);
SDL_RenderCopy(renderer, imageTextureRminitable, nullptr, &Minitbl);
break;
}
SDL_RenderPresent(renderer);
答案1
得分: 1
你的代码中有两次渲染主角twice。由于两种情况下条件完全相同,将它们合并成一个单独的情况:
if (mainchar.y > 250) {
// 主角在椅子上方,先渲染椅子
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
}
else {
// 主角在椅子下方,先渲染主角
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
}
英文:
You're rendering the main char twice in your code. Since the condition is exactly the same in both cases, combine them into a single case:
if (mainchar.y > 250) {
// Main character is above the chair, render chair first
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
}
else {
// Main character is below the chair, render main character first
SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论