AndEngine Wait then move character - andengine

I need Suggestion in how can i move my in a step by step move.
i know it incomplete.
need suggestion on delay
for (int redmove = Red_Current_Location; redmove < Red_Moving_Location; redmove++) {
RedCoinSprite.setPosition(Numbers_Text[redmove].getX(), Numbers_Text[redmove].getY(
thread.sleep(1000);//kinda like this,Because this is very worng.
}

spritemoveHandler= new TimerHandler(time, true,
new ITimerCallback()
{
#Override
public void onTimePassed(TimerHandler arg0)
{
MoveModifier mod=new Modifier(time-1,fromPoint,toPoint);
sprite.registerEntityModifier(mod);
}
});
scene.registerUpdateHandler(mObstacleCreatorTimeHandler);
use this handler to move sprite wait for second again move sprite on conti. time interval.

is This
MoveByModifier RedMoveto = new MoveByModifier(100,32*5,32*5);
RedCoinSprite.registerEntityModifier(RedMoveto);

Related

Unity | instantiate only works once

For some reason whenever I Instantiate, I can only instantiate once per build/run/game. I've tried to see if it was that I could only use different prefabs, but that isn't it. Please help, I've been trying to find an answer but no one seems to have the same problem. Thanks for reading this.
heres the project (assets and project settings) if you wanna mess around with it yourself
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreatePipe : MonoBehaviour
{
public GameObject Pipe;
public bool yes = false;
// Start is called before the first frame update
void Start()
{
StartCoroutine("initializationn");
Debug.Log("started");
}
// Update is called once per frame
void Update()
{
// if (yes){
// inst();
// yes = false;
// }
}
public IEnumerator initializationn()
{
Debug.Log("started");
inst();
// yes = true;
yield return new WaitForSeconds(3);
inst();
// yes = true;
yield return new WaitForSeconds(3);
inst();
// yes = true;
yield return new WaitForSeconds(3);
// while (true){
// inst()
// yield return new WaitForSeconds(3);
// }
}
public void inst(){
Debug.Log("in");
var g = Instantiate(Pipe, new Vector3(7f, Random.Range(-6.5f,0f), 0f), Quaternion.identity);
g.SetActive(true);
g.name = "Pipe";
}
}
The reason only one pipe exist at at time is that the script "PipeStuff" you attached to the Prefab "Pipe", makes it a singleton. So every time you create a duplicate instance you are deleting that object since a "PipeStuff" already exist.
Suggestions:
Your script is absolutely fine there is no problem with it but perhaps you are destroying that gameObject which is intantiating those pipe prefabs thats why its not working as you intend it to, any ways check if it is so because there seems to be no other problem with your script and also you can do it in one more way by using invokeRepeating function too like:
InvokeRepeating( nameof(inst), 0, 3 );
it will call this function repeatadely after every 3 seconds and the first parameter which is 0, is used for delay at the start so if you want to Invoke that function after some seconds you can do so as well and when you are done you can destroy or disable that gameObject too, its just a basic way of doing so otherwise there are tons of ways to do so anyways...
Hope it helps... Happy Coding :)

TrailRenderer start point on click

I followed Brackeys tutorial on how to create a Fruit Ninja Replica (youtube).
When creating the blade, though, the behaviour I got wasn't exactly the same.
Expected behaviour
Actual behaviour
The difference is that in the Actual behaviour, the trail starts where it stopped the last time it was shown. The code responsible for this is exactly the same as the video:
public class BladeController : MonoBehaviour
{
bool isCutting = false;
public GameObject bladeTrailPrefab;
GameObject currentBlade;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) {
StartCutting();
} else if (Input.GetMouseButtonUp(0)) {
StopCutting();
}
if (isCutting) {
UpdateCut();
}
}
void UpdateCut()
{
GetComponent<Rigidbody2D>().position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void StartCutting()
{
isCutting = true;
this.currentBlade = Instantiate(bladeTrailPrefab, transform);
}
void StopCutting()
{
isCutting = false;
Destroy(currentBlade, 1f);
}
}
After understanding the code, I thought the problem was that I instantiated the bladeTrail before actually moving the Blade to the new position, but tried moving the Instantiate method to UpdateCut after changing the position and only if this.currentBlade == null.
I've search a lot about this, and even found some other posts with the same problem but no answer.
It seams the Instantiate is using the last mouse position to instiantiate the prefab.
Maybe use:
Instantiate(bladeTrailPrefab, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity)
I ran into this problem following the tutorial as well.
It's a few years later but for those of you who hit this page, I found a solution for me that while isn't fantastic, it's better than having the streaks shown in the post.
Before instantiating the trail vfx, make sure to wait until fixed update is called after you set the position of the parent transform.
I did this with a Coroutine like so:
private IEnumerator StartCutting()
{
// When we begin cutting, move the blade object to the input position
m_isCutting = true;
m_previousPosition = m_camera.ScreenToWorldPoint(Input.mousePosition);
m_rigidBody.position = m_previousPosition;
// Then for positions to be updated so that the vfx doesn't get confused
yield return m_waitForFixedUpdate; // new WaitForFixedUpdate(); <-- cache this
// Instantiate the trail at this new position
m_currentTrail = Instantiate(m_trailPrefab, m_rigidBody.transform);
m_collider.enabled = false;
}

Delaying Update() with Invoke

Is it possible to delay the effects of a script that is attached to a game object?
I have 2 characters in a scene, both receiving live motion capture data, so they are both animated identically. I have rotated one by 180 degrees and placed it in front of the other one, to create the impression that they are copying/mirroring each other. Each has several scripts attached to them, of course.
Now, I would like to be able to delay one character a bit (e.g. 1 sec) to make it look more realistic, as if one character is observing the other character and then copying its exact movements.
I am trying to achieve this by buffering the animation data (network data) that reaches the second character. Below, is the code snippet that animates the 1st character immediately:
void Update()
{
Vector3[] latestPositions;
Quaternion[] latestOrientations;
if (mvnActors.getLatestPose(actorID-1, out latestPositions, out latestOrientations))
{
updateMvnActor(currentPose, latestPositions, latestOrientations);
updateModel(currentPose, targetModel);
}
}
And below, is the code snippet that is supposed to animate the 2nd character but in fact there is no movement at all:
private Queue<Vector3[]> posQueue;
private Queue<Quaternion[]> rotQueue;
void Start()
{
posQueue = new Queue<Vector3[]>();
rotQueue = new Queue<Quaternion[]>();
}
void Update()
{
Vector3[] latestPositions;
Quaternion[] latestOrientations;
if (mvnActors.getLatestPose(actorID-1, out latestPositions, out latestOrientations))
{
posQueue.Enqueue(latestPositions);
rotQueue.Enqueue(latestOrientations);
if ((posQueue.Count > 10) && (rotQueue.Count > 10))
{
Vector3[] delayedPos = posQueue.Peek();
Quaternion[] delayedRot = rotQueue.Peek();
updateMvnActor(currentPose, delayedPos, delayedRot);
updateModel(currentPose, targetModel);
}
}
}
Isn't peek supposed to provide the 1st element in the queue, which here would be the one in the 11th place? Or is it because queue is not suitable for heavy animation updating?
EDIT: SOLVED
Added int delayedFrames = 30; to class variables (to replace number 10) and used Dequeue instead of Peek.
EDIT: Answer to #JTech's question isn't complete in this answer, look at the comments underneath his/her question for the final solution
I personally dislike WaitForSeconds, it has slowed my game way down once.
Try something like:
public List<Vector3[]> PreviousPositions;
public List<Quaternion[]> PreviousRotations;
private Vector3[] latestPositions;
private Quaternion[] latestOrientations;
void Start(){
PreviousPositions = new List<Vector3[]>();
PreviousRotations = new List<Quaternion[]>();
}
void Update(){
if (mvnActors.getLatestPose(actorID-1, out latestPositions, out latetOrientations))
{
updateModel(currentPose, targetModel);
//Add the position and rotationstuff to the 'buffer' lists here
Invoke("DoStuff", 1);
}
}
void DoStuff(){
updateMvnActor(currentPose, PreviousPositions[0] , PreviousRotations[0]);
PreviousPositions.RemoveAt(0);
PreviousRotations.RemoveAt(0);
}
OnApplicationQuit(){
CancelInvoke();
}
I don't think you can delay the Update method.
What you can use is WaitForSeconds, or you can test if 1 sec passed with an if statement based on Time.
Then execute the code to move the second character.
EDIT
I think a simple timer will do the job but you will need to take in account the 1 sec time when getting the latest positions/orientations.
public float delay = 1.0f;
private float timer = 0;
void Update()
{
Vector3[] latestPositions;
Quaternion[] latestOrientations;
timer += Time.deltaTime;
if (timer >= delay)
{
if (mvnActors.getLatestPose(actorID-1, out latestPositions, out latestOrientations))
{
updateMvnActor(currentPose, latestPositions, latestOrientations);
updateModel(currentPose, targetModel);
}
timer = 0;
}
}
Otherwise you can use InvokeRepeating with a delay as a parameter to repeat a method every X seconds.
To take only the actions made before the delay, you can register the time at the moment when it's done, 'remove' the actions made during the delay and take the actions made before it.
Or register the number of actions made by the first character during the delay and remove the same number of actions - the last ones - to your update on the second character. (seems easier/more simple)
Like this, I assume the second character will not do the last actions made during the delay, but will do it after.

How to add gui text at run time in unity

I have a problem to display a text at run time in my game for a second , so I want to know if its possible to add or remove a GUI label to the seen at run time ?
Do you mean something like this?
void OnGUI() {
if (textShouldBeShown) {
GUI.Label(new Rect(10f, 10f, 100f, 50f), "MyText");
}
}
GUI components get drawn on every frame. the OnGUI() function is just like the Update() function except that unlike the Update() function GUI components can be called in it.
You can think of OnGUI as a loop. It will call GUI components declared inside of it in order, then do it all over again every frame. So if you hook into this loop and block some components from being called at runtime, the very next frame those componants will not be rendered.
Here is a set of functions that can allow you to do this is a specified time:
private bool guiIsOn = true;
private void TurnOffGUIInSeconds(int seconds)
{
StartCoroutine(_TurnOffGUIInSeconds(seconds));
}
private IEnumerator _TurnOffGUIInSeconds(int seconds)
{
yield return new WaitForSeconds(seconds);
guiIsOn = false;
}
void OnGUI()
{
if(guiIsOn)
{
GUI.Label(new Rect(5,5,5,5), "Label text");
}
}

Multiple Move Fx in GXT3

When I press a button I want a container to move in a direction, and after that to do another move. The problem is that only one move command is executed (the second one).
centerContainer.getElement().<FxElement>cast().setXY(bounds.getX() - compWidth, bounds.getY(), new Fx(800));
centerContainer.getElement().<FxElement>cast().setXY(bounds.getX(), bounds.getY()+500, new Fx(800));
How can I do two moves, one after another ?
Thank you,
Horatiu
Fx fx = new Fx(INTERVAL);
fx.addAfterAnimateHandler(new AfterAnimateEvent.AfterAnimateHandler() {
#Override
public void onAfterAnimate(AfterAnimateEvent event) {
//second move implementation
}
});