如何同时做两个if
How to do two ifs at once
我有一张背景图片和一张主图。在背景中,520(x) 和 260(y) 处是一座房子。我不希望我的英雄能够 "pass through" 房子,但我的方法不起作用。这是我到目前为止所做的
(不知道信息够不够):
public void moveRight() {
if (HeroX < 730) {
if (HeroX >= 520 && HeroY >= 260) {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX = HeroX + HeroSpeed;
}
} else {
System.out.println("Da kann ich nicht weiter gehen!");
}
}
房子是(我想)矩形或多边形而不是直线,那么你为什么不简单地使用其中之一 类 来表示房子?
//import java.awt.*;
//change the type of house to Polygon, if Rectangle doesn't meet
//your requirements
Rectangle house = new Rectangle(/* insert coordinates here*/);
Rectangle hero = new Rectangle(/* coordinates of the hero*/);
if(house.intersects(hero.x , hero.y , hero.width , hero.height))
System.out.println("Can't pass");
else
//move
你应该问问你的英雄+你的英雄移动速度是否在条件
if( (HeroX + herospeed ) >= 520 & HeroY >= 260 )
所以你检查他在搬家之前不会穿过房子
public void moveRight()
{
if (HeroX < 730) //i guess this is the border
{
if(HeroX >= 520 && HeroY >= 260) //this is the house at this position the hero should be already inside, so you have to rework this part
{
System.out.println("X = " + HeroX + " , Y = " + HeroY);
}
else
{
// here should be the error and also a second if when the hero is farther than the house length
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX = HeroX + HeroSpeed;
}
}
else
{
System.out.println("Da kann ich nicht weiter gehen!");
}
}
我认为这样更好:
public void moveRight() {
if (HeroX < 730) {
if (house.intersects(HeroX , HeroY, 38, 55)) {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX = HeroX + HeroSpeed;
}
} else {
System.out.println("Da kann ich nicht weiter gehen!");
}
}
public void moveLeft() {
if (HeroX > 0) {
if (house.intersects(HeroX, HeroY, 38, 55)){
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX -= HeroSpeed;
}
}else {
System.out.println("Da kann ich nicht weiter gehen!");
}
}
public void moveUp() {
if (HeroY >= 0) {
if (house.intersects(HeroX, HeroY, 38, 55)){
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroY -= HeroSpeed;
}
}
}
我有一张背景图片和一张主图。在背景中,520(x) 和 260(y) 处是一座房子。我不希望我的英雄能够 "pass through" 房子,但我的方法不起作用。这是我到目前为止所做的 (不知道信息够不够):
public void moveRight() {
if (HeroX < 730) {
if (HeroX >= 520 && HeroY >= 260) {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX = HeroX + HeroSpeed;
}
} else {
System.out.println("Da kann ich nicht weiter gehen!");
}
}
房子是(我想)矩形或多边形而不是直线,那么你为什么不简单地使用其中之一 类 来表示房子?
//import java.awt.*;
//change the type of house to Polygon, if Rectangle doesn't meet
//your requirements
Rectangle house = new Rectangle(/* insert coordinates here*/);
Rectangle hero = new Rectangle(/* coordinates of the hero*/);
if(house.intersects(hero.x , hero.y , hero.width , hero.height))
System.out.println("Can't pass");
else
//move
你应该问问你的英雄+你的英雄移动速度是否在条件
if( (HeroX + herospeed ) >= 520 & HeroY >= 260 ) 所以你检查他在搬家之前不会穿过房子
public void moveRight()
{
if (HeroX < 730) //i guess this is the border
{
if(HeroX >= 520 && HeroY >= 260) //this is the house at this position the hero should be already inside, so you have to rework this part
{
System.out.println("X = " + HeroX + " , Y = " + HeroY);
}
else
{
// here should be the error and also a second if when the hero is farther than the house length
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX = HeroX + HeroSpeed;
}
}
else
{
System.out.println("Da kann ich nicht weiter gehen!");
}
}
我认为这样更好:
public void moveRight() {
if (HeroX < 730) {
if (house.intersects(HeroX , HeroY, 38, 55)) {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX = HeroX + HeroSpeed;
}
} else {
System.out.println("Da kann ich nicht weiter gehen!");
}
}
public void moveLeft() {
if (HeroX > 0) {
if (house.intersects(HeroX, HeroY, 38, 55)){
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroX -= HeroSpeed;
}
}else {
System.out.println("Da kann ich nicht weiter gehen!");
}
}
public void moveUp() {
if (HeroY >= 0) {
if (house.intersects(HeroX, HeroY, 38, 55)){
}else {
System.out.println("X = " + HeroX + " , Y = " + HeroY);
HeroY -= HeroSpeed;
}
}
}