TranslateTransition jumps over a few pixels - animation

When i click and the animation begins, the image jumps from point A a quarter of the distance it should go and then runs just fine untill it reaches point B does anyone know why?
This is my methot i use for the image movement:
public void sky(Node node, double xDest, double yDest) {
TranslateTransition tTrans = new TranslateTransition(
Duration.seconds(4), node);
// tTrans.setFromX(xPlec);
tTrans.setToX(xDest);
tTrans.setRate(2);
tTrans.setInterpolator(Interpolator.LINEAR);
// tTrans.setFromY(yPlec);
tTrans.setToY(yDest);
tTrans.setRate(2);
tTrans.setInterpolator(Interpolator.LINEAR);
node.setLayoutX(node.getLayoutX() + xDest);
node.setLayoutY(node.getLayoutY() + yDest);
tTrans.play();
}

Why do you set end point for you node before starting transition? Try next code:
tTrans.setFromX(node.getLayoutX());
tTrans.setToX(xDest);
tTrans.setRate(2);
tTrans.setInterpolator(Interpolator.LINEAR);
tTrans.setFromY(node.getLayoutX());
tTrans.setToY(yDest);
tTrans.play();
Also note you don't need to call setRate and setInterpolator two times.

Related

How can I ensure the missile stays on the screen when non-spacebar keys are pressed?

I need to write a Processing program so that "When the user presses the space key a missile is fired from the front of the ship and moves rightwards until leaving the screen. The user can't fire another missile until the first is off the screen."
What I have written doesn't allow the missile to stay on the screen if other keys are pressed. I have tried adding the function under void keyPressed() with boolean true/false statements which only stopped the whole program from working.
How can I make sure it stays on the page? Also, how can I make sure the player can't shoot another missile until their previous one moves off the screen?
This is the code I have:
//global
int ship_y;
int angle;
int missileX;
void setup(){
size(500, 500);
ship_y = 10;
missileX = 55;
}
void draw(){
background(175);
fill(50);
rectMode(CENTER);
rect(30, ship_y, 50, 25);
//missile moves rightwards after spacebar is pressed
if (key == ' '){
circle(missileX, ship_y, 25);
missileX = missileX + 1;
}
}
//ship moves down when 's' is pressed and up when 'w' is pressed
void keyPressed(){
if (key == 's'){
ship_y = ship_y + 2;
}
if (key == 'w'){
ship_y = ship_y - 2;
}
}
Code within the draw() function runs every frame.
So every frame, you're clearing the background, drawing the ship, and drawing the missile. Good.
But.
You put the missile drawing code inside this if statement:
if (key == ' '){
that only draws the missile if the most recent key pressed was space. So as soon as you you press a different key, key is no longer equal to space and your missile doesn't get drawn.
One way to fix it would be to create a variable to indicate whether or not the missile was fired, and use that to determine whether or not to draw the missile (instead of checking the key variable).
Like this:
if (missileFired == true){
circle(missileX, ship_y, 25);
missileX = missileX + 1;
}
Code inside the keyPressed() function only runs once each time a key is pressed. This would be a better place to detect when the spacebar is pressed and turn on the missileFired flag:
void keyPressed(){
// code for other keypresses...
if(key == ' '){
missileFired = true;
}
}
To detect when the missile goes off screen, you'll just need to check whether missileX is greater than the width of the window. draw() would be a good place for that, since you probably want to check for it every time the missile moves.
When missileX gets larger than the window width, you can reset missileFired to false so a new missile can be fired. You'll also need to reset missileX to its original position so it originates from the ship position.

Unity2d game shooting and animation sync issue

I'm a new in Unity and making my first 2D game. I seen several topics on this forum in this issue, but I didn't found the solution.
So I have a lovely shooting animation and the bullet generation. My problem, I have to generate the bullet somewhere at the middle of the animation, but the character shoots the bullet and the animation at the same time, which killing the UX :)
I attached an image, about the issue, this is the moment, when the bullet should be initialized, but as you can see it's already on it's way.
Please find my code:
The GameManager update method calls the attackEnemy function:
public void Awake(){
animator = GetComponent ();
animator.SetTrigger ("enemyIdle");
}
//if the enemy pass this point, they stop shooting, and just go off the scren
private float shootingStopLimit = -6f;
public override void attackPlayer(){
//animator.SetTrigger ("enemyIdle");
if (!isAttacking && gameObject.transform.position.y > shootingStopLimit) {
isAttacking = true;
animator.SetTrigger("enemyShoot");
StartCoroutine(doWait());
gameObject.GetComponentInChildren ().fireBullet ();
StartCoroutine (Reload ());
}
}
private IEnumerator doWait(){
yield return new WaitForSeconds(5);
}
private IEnumerator Reload(){
animator.SetTrigger ("enemyIdle");
int reloadTime = Random.Range (4,7);
yield return new WaitForSeconds(reloadTime);
isAttacking = false;
}......
My questions:
- How can I sync the animation and the bullet generation ?
Why not the doWait() works ? :)
Is it okay to call the attackPlayer method from the GameManager update ?
The enemies are flynig from the right side of the screen to the left, when they reach the most right side of the screen, they became visible to the user. I don't know why, but they to a shooting animation (no bullet generation happen )first, only after it they do the idle. Any idea why ?
Thanks,
K
I would suggest checking out animation events. Using animation events, you can call a method to instantiate your bullet.
To use Mecanim Animation Events you need to write the name of the function you want to call at the selected frame in the "Function" area of the "Edit Animation Event" window.
The other boxes are for any variables that you want to pass to that function to trigger whatever you have in mind.
Triggering/blending between different animations can be done in many different ways. The event area is more for other things that you want to trigger that are not related to animation (e.g. audio, particle fx, etc).

How do i get the text of a UI element to move in Unity 4.6+?

When the player approaches an item I want some of it's properties to be displayed, but I don't want that info to cover up the player. I have the following code which does not work.
t = GetComponentInChildren<Text> ();
void OnTriggerEnter(Collider col){
if (col.gameObject.tag.Equals ("Player")) {
playerInRange = true;
col.GetComponent<Controller>().itemsInRange.Add(this.gameObject);
GetComponentInChildren<Canvas> ().enabled = true;
}
if (player.transform.position.x < this.transform.position.x) {
Debug.Log ("On yer right!");
t.rectTransform.position.Set(this.transform.position.x+50, this.transform.position.z, this.transform.position.z);
}
if (player.transform.position.x > this.transform.position.x) {
t.rectTransform.position.Set(this.transform.position.x-50, this.transform.position.z, this.transform.position.z);
}
}
The Debug.Log shows up, so all conditions are being met, but the text is not moving. Anyone have any ideas?
The issue is that the getter for tranform.position returns a 'copy' of the position Vector3, so calling Set will only change the 'copy' of that vector3 and not the position of the rect transform.
This is because Vector3 is a struct and not a class and therefore isnt passed via reference. https://msdn.microsoft.com/en-us/library/ms173109.aspx
so instead of calling set i would assign a new vector 3 to the position variable
t.rectTransform.position = new Vector3(transform.position.x-50, transform.position.y, transform.position.z);
If you're hitting the debug, can you verify the position you are displaying is where you expect it to be, currently the second and third parameters to the 'Set' method is:
this.transform.position.z
Wouldn't the middle one be expected to be position.y?

XNA game : calculate time between two shoots

I'm trying to make a game with the XNA library. I want a sprite to throw a fireball to hit falling asteroids. But I have a problem with pressing the concrete key: I want to throw fireballs, for example, with one second between throws.
I want to measure the time difference between creating instances. How can I do that?
UYou can use the ElapsedGameTime property of the gameTime variable passed to the Update method like this:
const float shootTimer = 1.0f;
float elapsedTime = 0f;
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
if(elapsedTime >= shootTimer && /* Your logic to see if you should shoot a fireball */ )
{
// Shoot the fireball!
elapsedTime = 0f;
}
base.Update(gameTime);
}
Basically, what you are doing in the above code is setting a minimum value (seconds) that need to pass between each shot.
Then you create a variable that will store the amount of time that has passed between each shot.
In the Update method, you add the time between each Update call and then check if it is bigger than the timer, and if it is, then you shoot and reset the elapsed time.
Note: I wrote that piece of code out of the top of my mind so it may have some minor issue.
Each call to Update of your main Game class or any GameComponent receives an instance of GameTime as an argument. Its property ElapsedGameTime can be used to accumulate the passage of time.

Lap timer in XNA 4.0?

Right, I've got a slight problem here, in which I've attempted to implement a lap timer.
In my protect override void update I've got this;
if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
{
redHit = true;
_timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
}
}
What I'm saying here^ is, if car2red is colliding with the starting line, the timer begins, but if it's not, timer does not add seconds (it just stops_ . How can I make it so, if car2red hits the startingLine and moves forward a few pixels (without touching starting line) the timer still continues?
Thank you.
You should have a separate if statement like this:
if (redHit) {
_timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
}
if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
redHit = true;
//Only use this line if you want to reset the timer to 0 when the player crosses that line again.
_timer1 = 0;// I'm assuming that _timer1 is a double
}

Resources