This site requires JavaScript, please enable it in your browser!
Greenfoot back
S77
S77 wrote ...

2017/4/22

hi, for some reason this code isnt doing exatly what i want

1
2
S77 S77

2017/4/22

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Lad here. * * @author (your name) * @version (a version number or a date) */ public class Lad extends Actor { /** * Act - do whatever the Lad wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkkey(); } public void checkkey() { if (Greenfoot.isKeyDown("d") && ("space".equals(Greenfoot.getKey()))) {setImage ("rightlad.gif"); shootright(); moveright(); } else if(Greenfoot.isKeyDown("d")) { setImage("rightlad.gif"); moveright(); } { if (Greenfoot.isKeyDown("a") && ("space".equals(Greenfoot.getKey()))) {setImage ("leftlad.gif"); shootleft(); if(getOneObjectInFront(wall.class)==null) moveleft(); } else if(Greenfoot.isKeyDown("a")) { setImage("leftlad.gif"); if(getOneObjectInFront(wall.class)==null) moveleft(); } if (Greenfoot.isKeyDown("w") && ("space".equals(Greenfoot.getKey()))) {setImage ("backlad.gif"); shootup(); moveup(); } else if(Greenfoot.isKeyDown("w")) { setImage("backlad.gif"); moveup(); } if (Greenfoot.isKeyDown("s") && ("space".equals(Greenfoot.getKey()))) {setImage ("forwardslad.gif"); shootdown(); if(getOneObjectInFront(wall.class)==null) movedown(); } else if(Greenfoot.isKeyDown("s")) { setImage("forwardslad.gif"); if(getOneObjectInFront(wall.class)==null) movedown(); } } } public void moveright() { setLocation(getX()+5, getY()); } public void moveleft() { setLocation(getX()-5, getY()); } public void moveup() { setLocation(getX(), getY()-5); } public void movedown() { setLocation(getX(), getY()+5); } public void shootleft() { getWorld() .addObject(new bulletleft(), getX(), getY()); } public void shootright() { getWorld() .addObject(new bulletright(), getX(), getY()); } public void shootup() { getWorld() .addObject(new bulletup(), getX(), getY()); } public void shootdown() { getWorld() .addObject(new bulletdown(), getX(), getY()); } private Actor getOneObjectInFront(Class wall) { GreenfootImage myImage = getImage(); int distanceToFront = myImage.getWidth(); int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation()))); int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation()))); return (getOneObjectAtOffset(xOffset, yOffset, wall)); } } when my game runs i want this code to make the player not be able to move through walls there isnt an error but instead just doesnt work as i want it to any help would be appreciated, thank you for your time!
Super_Hippo Super_Hippo

2017/4/22

#
What exactly is happening? You only have the condition (getOneObjectInFront method) when moving left or down, but not in the other two directions. Is that done on purpose?
S77 S77

2017/4/23

#
yes
S77 S77

2017/4/23

#
it just doesnt stop me from walking straight through walls!?
danpost danpost

2017/4/23

#
The rotation of the actor is always zero -- changing the image to face a different direction does not change the rotation of the actor. Therefore, your 'getOneObjectInFront' method is always looking to the right of the actor for a wall, which means the actor could walk down through a wall as long as no wall is immediately to the right of it. On top of that, it is coded only to work on the horizontal (left or right) and not on the vertical (up or down) as the width is used within the method to determine how far to look (the height would be needed to determine how far along the vertical).
S77 S77

2017/4/23

#
can you put that into code?
danpost danpost

2017/4/23

#
S77 wrote...
can you put that into code?
I did not give anything that could be written in code. I was just explaining why your actor was going through the walls. You will have to do something to correct it -- either re-writing the 'getOneObjectInFront' method or writing another method for vertical collision. BTW, the use of trig is not needed for an actor that only moves horizontally and vertically and whose collisions are only being checked for right and down. so the method should be at least simplified.
S77 S77

2017/4/26

#
hmmm is there any possible way to just flip the character instead of using the setRotation(); method, just for the sake of making it look nice!
danpost danpost

2017/4/26

#
You could probably use a combination of both:
if (getRotation() <= 180) getImage().mirrorVertically();
S77 S77

2017/4/28

#
thanks ill coment again if theres any problem
S77 S77

2017/4/28

#
ive got this test code here else if(Greenfoot.isKeyDown("a")) { setRotation(180); if (getRotation() <= 180) getImage().mirrorVertically(); if(getOneObjectInFront(wall.class)==null) moveleft(); } and it apears i cant take away the less than sign, so every time i hold a it spams flipping vertically! if it did not constantly flip it would be fine but right now its a bit of a problem
Yehuda Yehuda

2017/4/28

#
I don't think it's necessary to have an 'if' which checks the value of a variable, if the previous line sets the variable. So checking the rotation is useless since you know that it's 180. Since you have the flipping code and movement code in the same place, every time you want to move, the image will flip with that. So you should only flip the image when you switch directions, instead of constantly flipping the image while moving/pressing 'A'.
S77 S77

2017/4/30

#
it doesnt count as a statement if theres no if!
danpost danpost

2017/4/30

#
Use this:
else if(Greenfoot.isKeyDown("a"))
{
    if (getRotation() < 90 || getRotation() >= 270) getImage().mirrrorVertically();
    setRotation(180);
    if (getOneObjectInFront(wall.class) == null) moveleft();
}
You want the image to be upright when the rotation is 270 or 0 and upside-down when the rotation is 90 or 180. So, anytime the rotation changes from one of the sets to the other, the image needs flipped (mirrored).
Yehuda Yehuda

2017/4/30

#
S77 wrote...
it doesnt count as a statement if theres no if!
You can call that block of code whatever you want but what I said still holds true with your given code.
There are more replies on the next page.
1
2