A relatively simple question ( I think)

I'm pretty new to java. I've read a few books and been practicing the basics, and now I'm attempting to learn a bit more through the creation of a simple game. I've been able to get a lot of the groundwork down, however, I'm having trouble figuring out just how to do one of the simple things that I need.
This game is grid based, and each time a sprite moves it moves a uniform distance of one tile (around 20 pixels). I need to set it up, however, so that the characters don't just "pop" to each location, but that they actually use their walk animation to travel one square's worth of distance.
I'll post the simple code I tried, along with my thoughts on why it doesn't work, and then I'd like to know if I'm just going about it all wrong, and I should try another method, or there is an error in my code. My comments in the code explain what's going on (or supossed to be).
public void moveLeft() { //this method is called when the left arrow is pressed
    moving(); //this is a method that simply sets the sprite's animation to a walk loop and changes a couple status variables
    int x = getXPosn(); //get the current x position
    int y = getYPosn(); //get the current y position
    int newx = x - 20; //newx is where I want the sprite to end up
    for (int n = x; n > newx; n--) { 
        setPosition(n, y);
    stopLooping(); //stops the looping animation
}Now that seems pretty straightforward to me. I set the sprite to its walking animation. I get the current position, and tell it to go 20 pixels to the left, by decrementing 1 pixel at a time. Then, when it gets there, stop looping the animation.
However, what I end up with is the same as if the for loop is nonexistant and I just used setPosition() to "warp" the character to the new spot. The animation never takes place, and the sprite doesn't "slide" along the screen like I want. Is this because the for loop just processes so quickly from x to (x-20) that its unnoticable? And if so, how do I slow it down so that I can see this animation?
Perhaps I'm going about it all the wrong way, and there is a better method for getting at what I want. Any help would be greatly appreciated. For what its worth, I do know the moving() method,looping, stopLooping(), setPosition(), and all of that works. When I set it so that holding down an arrow key moves the sprite, it works fine. The sprite moves around, with the walk animation as long as the key is being pressed. That's just not what I need to do here. I just want it to walk a fixed distance, with its animation, from a key press.

well, it may have bloated my code a bit, but it works. I believe that once I get more of the game elements in place, I'll be able to simplifiy it, but I'm trying to take my time and get each part working solidly before adding more.
//I have a method that updates my sprite's x,y position each time through the gameloop by simply doing
locx += mx
locy += my
//where locx, locy is the current location and mx, my is the amount of movement (either positive or negative);
//Then, I needed 2 methods to get it to stop when I wanted.
public void stepLeft() {
       int x = getXPosn(); //returns the value of the current x position
       moving();
       setStep(-1,0); //this method sets the movement amount each update- (-1,0) means move left 1 pixel on the x axis each update
       moveLeft((x - 20)); //sends the value of where I want the sprite to end up when I'm finished to the other method I created
public void moveLeft(int x) {
       while (getXPosn() > x) { //this loop runs while the x location of the sprite is greater than the location I want it to end up at.
           isStill = false; //this is just a boolean identifier
       stopMove(); //A method that just stops the looping animation and resets the movement to (0,0)
}I tried to get it all in one method, but was having trouble, with using getXPosn() in the same method that I needed to check getXPosn() later. It kept creating infinite loops, because (getXPosn() - 20) would change based on the sprite's movement. So my workaround was to send it to another method for the loop.
As I said, I think this is a little bloated for what the code will be when I'm finished. I need to turn my background into a grid and I'm going to define each tile at some point. Then, I can go back and alter this code so that it just checks if its going into the boundaries of a new tile, rather than needing to use so many methods.

Similar Messages

  • First time computer builder - simple question(I think)

    Im putting together my first computer so far without much problem
    This is hardware I have
    380W Antec Power
    865 neo2 board
    2.8 p4 processor
    2x512 mb corsair memory
    lite on cd-rw
    plextor dvd-rw
    200gb western digital special additon hard drive.
    (which came with ata ultra controller card)
    My question is this:
    How should I set up my two optical drives and the hard drive on my available ide connections. Of course I have the two regular ide ports 1&2 and I have the ide3 next to the serial ata ports. I also have the ata ultra controller card plugged into one of the pci ports which gives me two more ide ports. I ve read through the forum listings and have not came across a simple description of how I should configure my hard drive and optical drives. How should I hook up these drives and what settings should I use (master/slave). Any help would be appreciated If it seems like I'm asking dumb questions it's just because I'm real new at this and want every thing to be perfects. thanks

    IMHO
    I would take out ata controller card. Your MB has enough....
    Primary IDE - master  - your WD harddrive
    Primary or Secondary IDE - slave - you lite on cd-rw
    Secondary IDE - master - plextor dvd-rw
    If you will install XP + service pack 1 your harddrive should be OK.
    PS - Just finish to build  about the same config....
    But I made my dvd-rw - external
    added later AFAIK Your 3rd IDE will not support optical drives.
    It is possible to put bootable harddrive on third IDE, but I it is not very simple
    and have no big advantages

  • Simple question I think, add JPanel to JScrollPane

    I would imagine that for anyone with experience with Java this will be a simple problem. Why can't I add a JPanel to a JScrollPane using myScrollPane.Add(myPanel); ?? it seems to get hidden...
    This works correctly:
         //Create a panel
         JPanel panel = new JPanel(new FlowLayout());
         //Make it pink
         panel.setBackground(Color.pink);
         //Make it 400,400
         panel.setPreferredSize(new Dimension(400,400));
         //Create a Scrollpane
         JScrollPane scrollPane = new JScrollPane(panel);
         //Make it 300,300
         scrollPane.setPreferredSize(new Dimension(300,300));
         //Create a JFrame
         JFrame frame = new JFrame("Test Scrollpane");
         frame.setLayout(new FlowLayout());
         //Set close operation
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //Set its size
         frame.setSize(600,600);
         //Add Scrollpane to JFrame
         frame.add(scrollPane);
         //Show it
         frame.setVisible(true);
    This doesnt:
         //Create a panel
         JPanel panel = new JPanel(new FlowLayout());
         //Make it pink
         panel.setBackground(Color.pink);
         //Make it 400,400
         panel.setPreferredSize(new Dimension(400,400));
         //Create a Scrollpane
         JScrollPane scrollPane = new JScrollPane();
         scrollPane.add(panel);
         //Make it 300,300
         scrollPane.setPreferredSize(new Dimension(300,300));
         //Create a JFrame
         JFrame frame = new JFrame("Test Scrollpane");
         frame.setLayout(new FlowLayout());
         //Set close operation
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //Set its size
         frame.setSize(600,600);
         //Add Scrollpane to JFrame
         frame.add(scrollPane);
         //Show it
         frame.setVisible(true);

    rcfearn wrote:
    I would appreciate it you could read the sample code, I am asking the question as to why I have to do this? For structural reasons I don't want to have to add the JPanel during instatiation of the JScrollPane...Please read the [jscrollpane api.|http://java.sun.com/javase/6/docs/api/javax/swing/JScrollPane.html] To display something in a scroll pane you need to add it not to the scroll pane but to its viewport. If you add the component in the scroll pane's constructor, then it will be automatically added to the viewport. if you don't and add the component later, then you must go out of your way to be sure that it is added to the viewport with:
    JScrollPane myScrollPane = new JScrollPane();
    myScrollPane.getViewport().add(myComponent);

  • OAM 10g - Simple question (I think)

    Hey guys, I want to run something by you to see if I'm missing something critical. What I'm trying to do is fairly simple...
    I want 2 login forms for the same webpage, depending on which hostname is used.
    internal.mydomain.com
    external.mydomain.com
    both of these point to an IIS box, which is configured to receive on *:80.. no hostheaders defined.
    Originally, there was only the internal.mydomain.com and that is all setup and working. Accessing http://internal.mydomain.com/secret brings up my "Custom Internal Form".
    I want to add the ability so that when http://external.mydomain.com/secret is acccessed, it brings up my "Custom External Form".
    I have set up a new Host Identifier with external.mydomain.com
    I have copied the policy domain to "My Policy - External" and set it to the new Host Identifier, and specified to use Basic Over LDAP (as a quick test, will create the actual form later)
    When I access external.mydomain.com/secret I keep getting the "Custom Internal Form".
    When I perform an Access Test within oam to the same URL, it shows the correct ("My Policy - External" ) policy domain in the evaluation result, aka.
    This tells me that OAM is configured correctly, but the WebGate isn't reporting properly the host header used.
    Any ideas? I would very much appreciate any insight or suggestions.
    Thanks very much

    Hi Alex,
    Couple of things:
    1. Please check if there is any proxy configured in external.domain.com to route requests to internal.domain.com
    2. Is Login page residing in same webserver as protected page? If so, it will be different for internal and external domains as it will be seperated by a firewall. I would configure a single authentication scheme without specifying anything in challenge redirect field so that Login page will be thrown from the webserver where it is called for secured page.
    3. Please check whether you are able to access login pages of both external and internal domains directly and check for any errors in webserver logs.
    Please post the results.
    Hope this helps!
    -Mahendra

  • Relatively simple question- I'm a bit new at this

    Hello folks
    I have a Hebrew text in a txt file that I need to transfer to another empty txt file. My question is how do I know which encoding this Hebrew is in? What must I use in order to properly work with Hebrew in my OutputStream and InputStream?
    Many thanks.
    Ilan

    Thanks noah, but I've already tried that. What I end up with is data that is not coded correctly in the result file. Here is what I've come up with so far: "list1" is the Hebrew list, while "list2" is in English, and needs no special encoding:
    import java.io.*;
    import java.util.*;
    public class combineLists
         public static void main(String[] args) throws IOException
         String newLine=System.getProperty("line.separator");
         StringBuffer buffer = new StringBuffer();
    try {
    FileInputStream fisH = new FileInputStream("list2.txt");
    FileReader fisE = new FileReader("list1.txt");
              BufferedReader buffEng= new BufferedReader(fisE);
              InputStreamReader isrH = new InputStreamReader(fisH, "ISO_8859-9");
    BufferedReader inH = new BufferedReader(isrH);
              FileOutputStream fos = new FileOutputStream("final.txt");
    Writer out = new OutputStreamWriter(fos,"ISO_8859-9");
    int ch;
    while ((ch = inH.read()) > -1 ) {
                   out.write(inH.readLine()+newLine+buffEng.readLine());
    inH.close();
              out.close();
    } catch (IOException e) {
    e.printStackTrace();
         // try {
    //FileOutputStream fos = new FileOutputStream("final.txt");
    // Writer out = new OutputStreamWriter(fos,"ISO_8859-9");
    // out.write(buffer.toString());
    // } catch (IOException e) {
    // e.printStackTrace();
    again, my results here are a list of part-English, part-garbage. See what you can make of it.
    Thanks
    Ilan

  • A very simple question (I think)

    My school has purchased some mac minis to use in our science lab, and now I want to get some very cheap monitors as we have a limited budget. I have some very basic questions that some experienced users could help me with:
    1. Could I use a TV monitor and connect via the HDMI input?
    2. What are the drawbacks when connecting a VGA monitor via the HDMI or mini connector?
    3. Would spending a bit more and getting a DVI capable monitor connected by the supplied "dongle" be best?
    4. What are the best brands of monitor?
    Many thanks,
    Geoff

    Well...
    1. If the TV has HDMI input, absolutely. Your main concern is the level of compatibility with a 'computer type' HDMI input, to avoid overscan/underscan issues.
    2. You can not connect VGA to HDMI without a device converting the signal from digital to analog, and such would cost much more than its worth. The only way to connect VGA avoiding compatibility issues is via the Mini Displayport with the correct dongle from Apple.
    3. I don't see any special advantage to getting a DVI capable computer monitor, over using an HDTV with HDMI or a computer monitor with HDMI. Basically you just need to find whatever display supports the input best as you need, at the best price.
    I won't really answer 4 because there's a lot of opinion in that.

  • Context help (please help, simple question i think)

    Hi, on my webserver, I have a nested tag that goes:
    <Host name="www.mydomain.com">
    <Alias>domain.com</Alias>
    <Context docBase="/home/domainacct/public_html/jsp" path="/jsp" reloadable="true">
    </Context>
    </Host>
    When I use this setup, JSP works! I can access any http://www.mydomain.com/jsp/*.jsp
    But I don't want JSP files to be loadable only from the /jsp directory, as I would like
    to be able to do:
    <Host name="www.mydomain.com">
    <Alias>domain.com</Alias>
    <Context docBase="/home/domainacct/public_html" path="" reloadable="true">
    </Context>
    </Host>
    so that I can access jsp files like: http://www.mydomain.com/test.jsp
    But when I try that setup it doesn't work.
    Do you guys know how I can do this? Thanks.

    nope u can't do that i far as i know.
    the context docBase should be the actual path to the folder. so if your webapp is in C:\TOOLS\webapps\Development than ur docBase is Development....like the root directory for example.
    the context tag as far as i know cannot be used for what u want 2 do. i don't think u can in Java.

  • A simple question I think

    I shot my movie on a Canon XL-1 which is 29.97 fps. I was turned down by a distributor because he told me 30 fps is a hard sell in today's market.
    I did not tell him the movie was shot 30fps, but he knew.
    If I had put my completed movie through cinema tools and exported a 24 fps movie, would it have made a difference?
    Thank you.

    24 fps imaging looks different from 30 fps. 30 frames/sec will look smoother and more 'real'. Think video news.
    24 fps (the frame rate of film) has a slight jerkiness to it as there is a longer time between each frame. To some, this specific image rhythm says "film".
    Graeme Nattress makes a series of filters that will tweek the gamma as well as do the frame rate conversion from 27.97 NTSC to 24p.
    www.nattress.com - film look.
    Good luck.
    x

  • OLE2 ...simple question I think.

    Hi,
    I am trying to use the OLE2 built in to communicate with an API (not microsoft).
    Here's what I have so far...
    Declare
    application ole2.obj_type;
    args ole2.list_type;
    BEGIN
    application:=ole2.create_obj('faxapi.Application');
    Application.addrecipient 'Name', 'surname', '5551212,,,225')
    End
    It's telling me that 'addrecipient' is an 'invalid reference to variable application'
    What am I missing? AddRecipient is correct according to the API documentation I have. I have the API installed on local machine but I don't see how forms knows where it is. Do I need an entry in the registry or something?
    Thanks for you help.
    Bri.

    Application.addrecipient 'Name', 'surname', '5551212,,,225')
    is not valid PL/SQL which is why the compiler is getting upset.
    Break down what you need to do, in this case call a method AddRecipient on the application object
    so the code will probably look a little like this:
    Declare
      application ole2.obj_type;
      args ole2.list_type;
    BEGIN
      application:=ole2.create_obj('faxapi.Application');
      args := ole2.create_aglist;
      ole2.add_arg(args,'Name');
      ole2.add_arg(args,'Surname');
      ole2.add_arg(args,'5551212,,,225');
      ole2.invoke(application,'addrecipient',args);
    End;

  • Simple Question (i think)

    This is the start of my class file, in my uml diagram it states
    +Order()
    +Order(ex :int, mod:int, min:int, custCD:char)
    what's the point of having two order methods, one blank, and one with variables in it?
    public class Order
       // Private Fields
          private int extreme;
          private int moderate;
          private int minor;
          private char customerCode;
          private int applicableDiscount;
           public Order()
             extreme = 0;
             moderate = 0;
             minor = 0;
             customerCode = 'R';
           public Order(int ex, int mod, int min, char custCd)
          }

    I'd argue that you shouldn't have the no-arg constructor, because it means you're creating objects in an incomplete state, which is a bad thing usually.
    On the other hand, JavaBeans requires a no-arg constructor (if I recall correctly) so maybe that's why it's there.
    But then, sometimes people throw in no-arg constructors all the time because they don't know what they're doing. In the same way, sometimes people create getters and setters for all object attributes because they don't know what they're doing.

  • Very basic user, simple question (i think)

    Hi there,
    Very new to dreamweaver, im making a website at the moment, what i dont understand is i have a set template, which i have used for about 6 pages within the website.  On all the pages it is very zoomed in when i preview it in chrome, apart from one page, this page looks perfect, its in perspective and everything isnt too large like on the others.  Is there a way i can make them all follow this one?
    Nevermind that, sorted that
    How do i set the zoom of the page, so when anyone goes onto the page it appears in the correct zoom, rather than far too zoomed in like it does now

    Newtimething wrote:
    Nevermind that, sorted that
    How do i set the zoom of the page, so when anyone goes onto the page it appears in the correct zoom, rather than far too zoomed in like it does now
    Not a clue what your'e talking about. Is the browser set to zoomed by default? Sometimes it can be set in the last position you use it at so you need to re-set it, that' a possibility

  • Simple question I think ( for the expert users of this group)

    In a tabular layout form, when I insert a record, I want a database item, for example b1.aa ( b1 is the database block) to begin with 1( if there are no records inserted yet) and increases automatically, like a sequence.
    1,2,3, 4 ....
    how can I do this?
    in a when - new item instance trigger maybe?
    where? on the block or on the item?
    and which is the right code?
    Thank you all

    Hi Champion
    place ur code in when- new- record- instance at block level and increment it with the value required.
    Thank you
    Sasi

  • Unicode - Simple Question (I think)

    The following code is dynamically generated and executed.  I don't know what table I'm going to get until run-time when I gen this code.  I keep getting the following run-time error:  UC_OBJECTS_NOT_CONVERTIBLE.  Please propose code that works in a Unicode system.  Note that I need to pass the internal table "G" back through a function module interface so I need to have it reference a DDIC object statically.
    REPORT ZDYN1.
    DATA : BEGIN OF G OCCURS 0,
             DATA(5000),
           END OF G.
    PERFORM GET_DATA TABLES g .
    FORM GET_DATA TABLES PASSED_TAB STRUCTURE g.
      TABLES: VBAK.
      FIELD-SYMBOLS <TTT> TYPE ANY.
      ASSIGN VBAK TO <TTT>.
      select distinct * INTO <TTT>
      FROM VBAK
      WHERE
      VBAK~VBELN EQ '0105045404'.
        PASSED_TAB = <TTT>.
        APPEND PASSED_TAB.
      ENDSELECT.
      LOOP AT PASSED_TAB.
        WRITE:/ PASSED_TAB.
      ENDLOOP.
    ENDFORM.                    "GET_DATA

    you wanted to select records from a table (table name known only at runtime - may be a user input) and pass it to a generic itab? right ? if so check out the code of RFC_READ_TABLE function module
    particulary from
    read data from the database and copy relevant portions into DATA
    Regards
    Raja

  • Disk space-simple question

    This is a simple question, I think: I have an external drive (250GB) and have my iPhoto and iTune libraries over there. If I delete about half of my photos from the main library (on the desktop, G4, running 10.4.10) to free up some space, will I still have all my photos from the original main library on the external drive, and just as important, will I still be able to update the external drive as I add new photos to the main library.
    Sorry, I know this is basic stuff but I want to be sure I can free up some space but not lose any photos in the process.
    Thanks, Mike

    Hi--
    If you delete files on the computer they will still remain on the external hard drive. HOWEVER, if you are using a backup program that updates the external drive to reflect the G4 then it may erase whatever you've deleted on the G4. It depends on how the backup program is set (if you are using one).
    If you are not using a backup program and just dragging the folders over to the external drive manually then you would have to be careful not to replace the folders that you want to keep on the external drive. Say for example you have your "Pictures" folder on the external drive. If you delete a bunch of photos on your G4 and then move the "Pictures" folder from your G4 to the external drive, it will REPLACE the "Pictures" folder on the external drive.
    One way to get around this would be to make a seperate folder on your external drive EACH time you made a backup, and drop your "Pictures" folder into that.
    Does this make sense?

  • A simple question on random number generation?

    Hi,
    This is a rather simple question and shows my newbieness quite blatantly!
    I'm trying to generate a random number in a part of a test I have.
    So, I have a little method which looks like this:
    public int getRandomNumber(int number){
            Random random = new Random(number);
            return random.nextInt(number);
        }And in my code I do int random = getRandomNumber(blah)...where blah is always the same number.
    My problem is it always returns the same number. What am I missing here. I was under the impression that nextint(int n) was supposed to generate the number randomly!! Obviously I'm doing something wrong or not using the correct thing. Someone please point out my stupidity and point me in the right direction? Ta

    I think the idea is that Random will generate the same pseudo-random sequence over and over if you don't supply a seed value. (The better to debug with, my dear.) When you're ready to put an app into production, the seed value should be the current system time in milliseconds to guarantee a new sequence with each run.
    Do indeed move Random outside the loop. Think of it like a number factory - instantiate it once and let it pump out the random values for you as needed.

Maybe you are looking for

  • Unable to open Document from Asset Business Package

    Hi, We have installed Asset Business Package in portal. Almost everything is working fine. But when we try to open a document (PDF ) attached to an asset by clicking the document link in the page, a new browser window opens with a text inside. The PD

  • Burn videos to dvd

    Is there a best way to burn itunes videos to a dvd? Will the dvd work? Thanks.

  • How to delete a photo from Device-Work/photos

    How to delete a photo from Device-Work/photosI saved a photo from my work email account (it's a picture of a golf green) and afterward, I noticed that I can't really do anything with it. How do you delete files from the "work" side of the phone? Also

  • ISE PSN rejecting RADIUS request

    Hi, We have a distributed ISE infrastructure version 1.3. We begin noticing the following problem. Randomly the PSN's started dropping radius requests. Basically they didn't serviced any client. It looked like this bug: ISE PSN rejecting RADIUS reque

  • How to run java exe files without having to install jre

    Hi all i want to run a java application. i already converted the jar file to exe using Launch4j but the problem is that i want to run the application on other computers without having to install jre. please, is this possible and if yes, how can i do