Another question about moving something. Parabolic path.

Started by HAL, Sun 15/02/2015 15:51:46

Previous topic - Next topic

HAL

Here's my code.

Code: ags


  int i = 0;
  float yy1 = 0.0;
  float xx1 = 0.0;

  while(i<451)
    {
    xx1 = IntToFloat(i);
    yy1 = -(((4.0/2025.0)*xx1)*(xx1-450.0));
       
    oBird1.Move(81+i,367+FloatToInt(yy1, eRoundNearest), 6, eBlock, eAnywhere);
    Wait(1);
    i++;
    }


This should generate a parabolic flight path of an upside down U-shape, moving from left to right, 450 pixels across the screen. (Assuming my maths is correct. Either way, it should generate a path of some sort).

It totally doesn't though. The bird blips a little, the loop then runs to the end with no movement, then when it's done the bird moves horizontally across the screen in one smooth final move to its end co-ordinate.

I'm trying all the techniques from the similar thread I made just yesterday, but always failing.

Have I missed something? :confused:

Thanks.

Matti

The problem is that you're using a loop that prevents the code from being updated. i is being increased until it reaches 450, then the code continues and the bird moves. Maybe just replace while with if?

MiteWiseacreLives!

Using move is frustrating try setposition, works much better.

Snarky

Yes, as Matti and Mite sort-of said, the problem is that Move() isn't a "move the object to this position, now"-command, but rather a "tell this object to start moving (in a straight line) to this position"-command. And as long as the loop is running, all those Wait()s is stopping it from actually making any progress, so it's only after the whole thing is done that it gets going properly.

So yes, use SetPosition(), or just set the oBird.X and oBird.Y coordinates directly.

HAL

You won't believe this.

In my previous thread my final working code used SetPosition(). :sealed:

I think I switched to Move() because it allows you to decide speed, so for some reason I had this idea it would mean I could change the speed at which the bird moved across the screen. Halfway through realised this wouldn't work because it's always gonna be going through 450 cycles so will always take ~11 seconds. Well, actually it wasn't working at all so I don't even know what I was thinking, hah.

At no point did it click that I needed to switch to SetPosition().

It works now. To change the speed I just made the loop counter increase by i = i+X  each time.

Thanks again :smiley:


Snarky

Many years ago when I was first learning AGS, this was almost exactly the first thing I tried to code (mine was a parabolic jump), and I made exactly the same mistake.

HAL

Maybe a few years down the line I'll be the one telling people how do it. Heh!

SMF spam blocked by CleanTalk