I am building a mobile game and trying to implement the same concept of image rotation as Flappy bird.
I am able to rotate the image counter clock with gravity and when I touch the screen the rotation is clockwise
My issue is that I am not getting a smooth rotation clockwise when I touch the screen to make the bird fly. The bird looks like it rocking like a boat.
I know it has to do with the origin but I am stuck here.
See my code
private void updateAnimation(){
stateTime += Gdx.graphics.getDeltaTime();
if(!Play.hit && Play.state != Play.GAME_OVER)
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
anim = new Sprite(currentFrame);
anim.setSize(BODY_WIDTH, BODY_HEIGHT);
anim.setOrigin(BODY_WIDTH/2, BODY_HEIGHT/2);
anim.setPosition(SCREEN_X-BODY_WIDTH/2, Player.position.y);
if(Play.state == Play.GAME_RUNNING ){
if(ANGLE >= -75){
if(!Gdx.input.justTouched()){
updateRotation();
}
}
anim.setRotation(getANGLE());
}
anim.draw(batch);
}
public void updateRotation(){
ANGLE = Player.velocity.y / 8;
}
this is the code for setting the rotation clockwise
if(GameWorld.ANGLE <= 10){
GameWorld.setANGLE(5* Gdx.graphics.getDeltaTime());
}
Player.velocity.y = 320;
Rotating the Texture region instead via the batch
Related
Hey everyone this one has me stumped. So I have an Array of Movie clips called sharkthat are added to the stage from either the left or the right side of the stage. Now when they are added I have them move in either the positive or negative x position, so in a Horizontal line. But to make the effect that the player is moving forward in a stream of water and all the other objects are passing the player I also have the array of shark move clips moving in the positive y position so moving vertically downwards. But when I do this have the values in my ENTER_FRAME the shark movie clips in the array appear to be moving Diagonal which is what I don't want at all. Does anyone know how to fix this to have the Movie clip move downwards and also in a straight line across the screen.
Here is what I have so far in my shark class:
private function startPosition():void
{
//Pick the frames for start position
var nRandom:Number = randomNumber(1, 2);
//Setup goto and stop on timeline
this.gotoAndStop(nRandom);
//Pick a random speed
nSharkSpeed = randomNumber(3, 5);
//Pick a random number for start position
var nLeftOrRight:Number = randomNumber(1, 2);
//If our nLeftOrRIght == 1 start on the left side
if (nLeftOrRight == 1)
{
//Start shark on left side and move to right
this.x = (stage.stageWidth / 2) - 240;
sSharkDirection = "R";
}else
{
//Start shark on right side and move to left
this.x = (stage.stageWidth / 2) + 240;
sSharkDirection = "L";
}
//Set y position
this.y = (stage.stageHeight / 2) - 400;
//Move our shark
startMoving();
}
private function startMoving():void
{
addEventListener(Event.ENTER_FRAME, sharkLoop);
}
private function sharkLoop(e:Event):void
{
//test what direction fish is moving in
//If fish is moving right
if (sSharkDirection == "R")
{
//Move Shark right
this.x += nSharkSpeed;
}else
{
//Move Shark left
this.x -= nSharkSpeed;
}
this.y += nSharkSpeed;
}
any help would be appreciated thanks!
It looks like your code does what it means to do: you've told that, firstly, you want sharks to move horizontally, and, secondly, vertically. Well, changing X and Y coordinates gives us diagonal move =)
So, it looks like your code is OK.
Maybe, you should rethink the logic of your application. Also, you may remove/comment the part of code where you change the X position of sharks:
if (sSharkDirection == "R")
{
//Move Shark right
this.x += nSharkSpeed;
}else
{
//Move Shark left
this.x -= nSharkSpeed;
}
And all sharks will move only by Y.
I'm working on a simple game in AS3 where a player should be able to jump on a box.
How to detect if the player landed on the box and didn't run into it?
AS3 to make the player jump:
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onDown(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.UP)
{
//umbau das mehrfach tab bis höhe erreicht?
if(jumping != true)
{
jumpPow = -10;
jumping = true;
}
}
}
function update(evt:Event):void
{
if(jumping)
{
player_mc.y += jumpPow;
jumpPow += grav;
if(player_mc.y >= stage.stageHeight)
{
jumping = false;
player_mc.y = stage.stageHeight;
}
}
}
This is how the game layout looks like: (the grey box is moving from right to left, the player's position is fixed)
You should start with a simple rectangle collision function:
/* Test collision between a rectangle and another rectangle. */
/* right() and bottom() return x+width and y+height of the parent rectangle. */
Rectangle.prototype.collideRectangle=function(rectangle_){
if (this.x>rectangle_.right()||this.y>rectangle_.bottom()||this.right()<rectangle_.x||this.bottom()<rectangle_.y){
return false;
}
return true;
}
if you are using the gray rectangle as a platform, you can do a simple test to see if the red rectangle is landing on the gray one by using its current and last position:
/* If true, the two rectangles are definitely colliding. */
if (red_rect.collideRectangle(gray_rect)){
/* Get the last position of the bottom of the red rectangle. */
var old_bottom=red_rect.bottom()-red_rect.velocity.y;
/* If the old bottom was above the gray rectangle on the last frame and is colliding in this frame, you know that the red rectangle is landing on the gray rectangle. */
if (old_bottom<gray_rect.y){
/* Place the red rectangle on top of the gray rectangle. */
red_rect.y=gray_rect.y-red_rect.height;
}
}
This is assuming you move the red rectangle by adding the value of velocity.y to its y position on each frame. Also, I'm using JavaScript, but AS3 and JavaScript aren't so different.
If you want more than a platform, and you want full collision, you should consider the penetration depth of the red rect on the gray rect and separate them on the axis with the smallest penetration depth. There are a lot of resources about this stuff online, and AS3 in particular has a lot of game related tutorials with a focus on do it yourself physics. Some really good collision methods are separation of axes theorem (SAT) and GJK (I'm not going to try spelling the names in GJK).
I have a turret I want to constrain rotation with the turret rotating and facing the mouse, so it doesn't go over the maximum rotation and stops rotating if you reach the maximum point. and does not go lower than minimum degrees when it reaches minimum degrees, but the max and min angles change depending on the side the mouse is facing.
When the mouse goes on the right side of the turret, aka mouseX is greater than turret's x position.
the turret will change scaleX and the turret turns to the right and has constrained aiming rotation between 120 degrees and -160.
when the turret faces left, aka the mouseX is less than the turret's x position. the turret will switch scaleX and turn to the left. then, the constrained aiming rotation switches to between 70 degrees and -20 degerees.
Here is my code, I'm new to calculating rotations and degrees so I think I got it completely wrong, because the turret doesn't rotate at all unless i remove the code. I need help on fixing the code. Thanks!
function update(e:Event)
{
//make the turret face the mouse
if (parent != null)
{
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy, dx)/Math.PI * 180;
//this is supposed to constrain the rotation if the mouse is bigger than turret's x
//position, else constrain it the other way, this part of the code doesnt work.
//this code makes it so the turret is unable rotate at all
//if i remove the code, the turret can rotate freely though.
if(mouseX > x)
{
angle=Math.min(angle,120);
angle=Math.max(angle,-160);
}
else
{
angle=Math.min(angle,-20);
angle=Math.max(angle,70);
}
rotation = angle;
}
}
You haven't specified how update is being called. If it's called by (say) a mouse move event, you can get the mouse coords directly from the event.
Here is another version which works for me using that technique. Script is in the timeline of the 'turret' movieclip.
function update(e:MouseEvent)
{
//make the turret face the mouse
if (parent != null)
{
var dx = e.stageX - x;
var dy = e.stageY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
//this is supposed to constrain the rotation if the mouse is bigger than turret's x
//position, else constrain it the other way, this part of the code doesnt work.
//this code makes it so the turret is unable rotate at all
//if i remove the code, the turret can rotate freely though.
if (mouseX > x)
{
angle = Math.min(angle,120);
angle = Math.max(angle,-160);
}
else
{
angle = Math.min(angle,-20);
angle = Math.max(angle,70);
}
rotation = angle;
}
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, update);
The objective is for the red circle move horizontally to the left and when completely off stage to reappear on the right side.
Why is it that setting the property X in ball.graphics.drawCircle (300, 300, 50); causes the ball to disappear before it reaches the stage left side?
If I leave x being 0 it behaves as I want
Class.
public function RedBall()
{
ball.graphics.beginFill (0xFF0000);
ball.graphics.drawCircle (300, 300, 50);
ball.graphics.endFill();
}
In my main
function update(e:Event):void {
a.ball.x -= 10;
if (a.ball.x < -a.ball.width) {
a.ball.x = stage.stageWidth + a.ball.width;
}
}
I think this probably must be some very obvious errors as I'm beginning to learn AS3 but if needed I can post the whole code.
You are drawing your circle at a point of 300,300, but you are not later accounting for that in your code where you check its position.
You can do one of two things.
Recommended:
Draw the circle at the point of origin, and place the instance of ball at an x of 300.
Alternative:
Use the ball's actual bounds to determine its position:
function update(e: Event): void {
ball.x -= 10;
if (ball.getBounds(stage).x < -ball.width) {
ball.x = stage.stageWidth - ball.getBounds(stage).x;
}
}
I have a bit of a problem with hitTestObject in Flash.
I have these tiles generated on the screen and I have a player added to the tiles. I created an invisible square bitmap for the player, which is the size of the tile so it can register whether it is hitting the tile completely. The problem is, it is registering hit test from tiles that aren't even next to the player. The link below, My mouse is over the tile that is highlighted, and I have a trace that checks the hitTest between the player and the current Tile that it's on.
http://postimg.org/image/6so3npm19/
Here is the code for the bitmap. I've been playing around with the x and y position and the size of it.
visionArea.graphics.beginFill(0x00FF00, 1.0);
visionArea.graphics.drawRect(0, 0, 85, 85);
visionArea.graphics.endFill();
var matrix:Matrix = new Matrix();
matrix.rotate(Math.PI / 4);
matrix.scale(1, 0.5);
visionArea.transform.matrix = matrix;
addChild(visionArea);
visionArea.mouseEnabled = false;
visionArea.visible = false;
visionArea.x = 4;
visionArea.y = -21;
When I click a tile and the player is next to it, I move it to that tile.
if (player.visionArea.hitTestObject(event.currentTarget as Tile))
{
player.x = (event.currentTarget.x)+55;
player.y = (event.currentTarget.y)+20;
}
I also have an enemy on the screen (the green tile). I'm trying to have the player not be able to go on the tile that the enemy is on, but sometimes it would work, sometimes it wouldn't.
if (enemy.enemyVisionArea.hitTestObject(event.currentTarget as Tile))
{
player.x != (event.currentTarget.x)+55;
player.y != (event.currentTarget.y)+20;
}
if (player.visionArea.hitTestObject(event.currentTarget as Tile) && !enemy.enemyVisionArea.hitTestObject(event.currentTarget as Tile))
{
player.x = (event.currentTarget.x)+55;
player.y = (event.currentTarget.y)+20;
}
I've never seen anyone use != as an assignment operator.