Sign in to follow this  
Thomas Wiborg

Target bounce away from wall

Recommended Posts

Thomas Wiborg    1573

Im very newbie and making a 2d spaceship game, similar to Stargunner.
I have target(enemies) coming from right side of screen and heading left, where my ship is at.
My targets are circles who randomly chooses Y coords from 0, 640.
The thing is. When one of my targets hit the upper "wall" i want them to bounce down.
And my problem is that i cant modify the value of Points. What can i do, is it even possible to modify Points values?

 

I have my properties in GameItem class.

 

"Cannot modify the return value of 'SpaceAttack.GameItem.Position' because it is not a variable"


 

 if (target.Position.Y < 0)
            {
                target.Position = new Point(this.target.Position.X, this.target.Position.Y = 0);

                target.Velocity = new Point(this.target.Velocity.X, this.target.Velocity.Y *= -1);
            }

 

 

Share this post


Link to post
Share on other sites
__SKYe    1784

You don't need to bounce enemies off walls.

 

If the enemies must appear within the range [0 - 640], then all you have to do is get a random value between 0 + enemyHeight/2 and 640 - enemyHeight/2 (the enemyHeight/2 in a circle is the radius).

This makes the enemy spawn inside the screen.

 

If what your asking is that you have an enemy that can move in the Y axis (up/down), and when it would go outside the screen you want to bounce it off on the walls, then a simple way to do it, is if you have a variable for the enemy's velocity(acceleration), when the enemy hits the wall, you change the velocity's sign. This will make the enemy suddenly move in the other direction, therefore bouncing it off on the wall.

 

I don't know what language you're working on, but to me it seems it is ActionScript.

 

If it is so, then perhaps you can go to http://asgamer.com/2009/flash-game-design-basics-adding-library-objects-to-stage-with-as3 to get a good tutorial on how to make spaceship game (although this is a vertical shooter) in AS3. It presents various concepts such as ship movement, the bouncing on walls, enemies, and even a star filled background.

This tutorials are separated into articles, so search them in the site.

 

If you're not using AS3, then perhaps you could show some more code, to see if i can help you more.

Share this post


Link to post
Share on other sites
BrianJensen    343

If your enemy is less than the screen min or greater than the screen max you would reverse the speed of x and/or y.

 

[code]xSpeed = 1; ySpeed = 1; If (target.x < screenMinX || target.x > screenMaxX)     xSpeed = -xSpeed; //reverse if(target.y < screenMinY || target.y > screenMaxX)     ySpeed = -ySpeed; //reverse</pre> target.x += xSpeed; target.y += ySpeed;[/code]

 

you mean that?

Edited by 0Circle0

Share this post


Link to post
Share on other sites
Khatharr    8812

Why can you not modify the point?

 

The code you posted in the OP was trying to replace the point, which could be no good for any number of reasons depending on the class. Can't you just

 

if(target.Position.Y < 0) {
  target.Position.Y = 0;
  target.Velocity.Y = -target.Velocity.Y;
}
Edited by Khatharr

Share this post


Link to post
Share on other sites
Mizu    1502

If target.Velocity is a C# Property then you won't be able to modify its members, since a Point in C# is of the struct data type. What this means is that target.velocity returns a temporary copy of your data, so modifying that has little meaning. The solution is to either just make velocity public, without any get:ers and set:ers, or create a method that allow you to set velocity, like so

 

public void SetVelocity(int velX, int velY)
{
      velocity.X = velX;
      velocity.Y = velY;
}

 

Why are you using Points by the way? I would suggest using Vector2 for things like speed and velocity. It's better suited for that kind of stuff

 

 

Hope that helps smile.png

Share this post


Link to post
Share on other sites
Thomas Wiborg    1573
If target.Velocity is a C# Property then you won't be able to modify its members, since a Point in C# is of the struct data type. What this means is that target.velocity returns a temporary copy of your data, so modifying that has little meaning. The solution is to either just make velocity public, without any get:ers and set:ers, or create a method that allow you to set velocity, like so

 

public void SetVelocity(int velX, int velY)
{
      velocity.X = velX;
      velocity.Y = velY;
}

 

Why are you using Points by the way? I would suggest using Vector2 for things like speed and velocity. It's better suited for that kind of stuff

 

 

Hope that helps smile.png

 

Well, Vector2 aint possible to modify like variables either?

Share this post


Link to post
Share on other sites
Mizu    1502
That's right. Vector2 are also a struct, so the solution is the same as those a suggested in my previous post smile.png Edited by Mizu

Share this post


Link to post
Share on other sites
Thomas Wiborg    1573

Yeah, but what you say, is that Vector2 is better to use than Point in games? Well I fixed it. And here is my new code. Thanks alot for the help guys! Realy amazing community here on gamedev!

 


            if (target.Position.Y < 0)
            {
                Vector2 vv;
                Vector2 vp;
                vv = target.Velocity;
                vv.Y *= -1;
                target.Velocity = vv;
                
                vp = target.Position;
                vp.Y = 0;
                target.Position = vp;            
            }
            else if (target.Position.Y > SCREEN_HEIGHT - target.Origin.Y)
            {
                Vector2 v2v;
                Vector2 v2p;
                v2v = target.Velocity;
                v2v.Y *= -1;
                target.Velocity = v2v;

                v2p = target.Position;
                v2p.Y = SCREEN_HEIGHT - target.Origin.Y;
                target.Position = v2p;      
            }

 

Edited by Thomas Wiborg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this