I have a conditional if then statement running where I want the animations to change if a certain condition is met. For some reason when it starts, the correct animation plays, but when it is changed the animation doesn't change. The code is running in LateUpdate(). I made a hack to test the changing of the animations and it won't work either.
Everything I have read says to use SetAnimation and it will over ride the previous animation, but this doesn't seem to be working. Here is the code to hack that result I want.
rightCrowdMemberAnimation.state.SetAnimation (0, "idle_07", true);
if (Input.GetKeyDown (KeyCode.A)) {
rightCrowdMemberAnimation.state.SetAnimation (0, "yay_07", true);
}
Thanks for the help.
What is weird is that the in the inspector it says that the animation has changed, but visually it hasn't changed.
Note: This is what i was trying to accomplish. It appears that while I was in LateUpdateit was resetting back to the idle when I clicked A. I am still not sure why it won't change the animation under what is below. I thought the A button trick was similar in execution, but I guess I was wrong.
void Start(){
Assume I did a random roll of color {red,blue,yellow}
}
The real issue is
void LateUpdate(){
if (color = red) {
rightCrowdMemberAnimation.state.SetAnimation (0, "idle_07", true);
} else if (color = blue) {
rightCrowdMemberAnimation.state.SetAnimation (0, "idle_08", true);
} else if (color = yellow){
rightCrowdMemberAnimation.state.SetAnimation (0, "idle_09", true);
}
}
In the inspector the animation I want to show up is there. Just the animation that I am calling isn't showing up.
Basically you have to change property AnimationName on SkeletonAnimation script. Here is sample:
private SkeletonAnimation skeletonAnimation;
void Awake()
{
skeletonAnimation = GetComponent<SkeletonAnimation>();
}
void LateUpdate(){
if (color = red) {
skeletonAnimation.AnimationName = "idle_07";
} else if (color = blue) {
skeletonAnimation.AnimationName = "idle_08";
} else if (color = yellow){
skeletonAnimation.AnimationName = "idle_09";
}
}
Related
I am using Processing to create a basketball game. I have managed to create the basketball game but I want to have a click to start home screen. I have made the graphic for the home screen but I am not sure how to integrate it into the game code. Any ideas on how to go about this. Thanks!
I found something on the internet related to this which was...
if (started) {
//all the code for the game
} else {
// all the code for the start screen
if (keyDown("enter")) {
started = true;
}
}
Im not sure if this is leading me in the right direction or how I could necessarily use this.
Use keyPressed() outside the draw().
void keyPressed() {
println(int(key));
if (key==10) {
println("ENTER");
}
}
key - contains the value of the most recent key on the keyboard that was used (either pressed or released).
What is described in: https://processing.org/reference/keyPressed_.html
Here is a little demo program that may help you. The global variable started controls whether the game has begun or not. The function keyPressed sets it to true if you press the ENTER key.
In draw, put your game code in the first if-part and your waiting screen in the second.
boolean started = false;
void draw() {
background(0);
textAlign(CENTER);
if (started) {
// your game code here
text("gameplay", 50, 50);
} else {
// your start/launch screen here
text("waiting...", 50, 50);
}
}
void keyPressed() {
if (keyCode == ENTER) {
started = true;
}
}
I'm constructing a custom node editor window in Unity, and I've had a look at various resources such as this one, which uses GUI.Box to construct node windows.
This works great, and I'm able to drag these windows around the way I want, however once I add controls to the GUI.Box, I want them to override the Drag() function I've written.
Here's an example of the issue - When I move the vertical slider up, the entire box drags with it.
Is there a way to fix this behavior using GUI.Box, or should I go back to GUI.Window with its built-in GUI.DragWindow()?
Here's a simplified version of the code I'm using:
EditorMouseInput.cs:
private bool ActionLeftMouseDown()
{
mouseDownNode = editor.GetSelectedNode(Input.current.mousePosition);
if (mouseDownNode == null)
editor.StartMovingEditorCanvas();
else
mouseDownNode.IsSelected = true;
}
BaseNodeEditor.cs:
public BaseNode GetSelectedNode(Vector2 mousePos)
{
foreach (BaseNode node in Nodes)
{
if (node.WindowRect.Contains(mousePos))
return node;
}
return null;
}
public void Drag(Vector2 delta)
{
if (!MoveEditorMode && !ConnectionMode)
{
foreach (BaseNode node in Nodes)
{
node.Drag(delta);
}
}
BaseNode.cs:
public void Drag(Vector2 delta)
{
if (IsSelected)
draggedDistance += delta;
}
The vertical slider is added in the derived JumpNode class. Extract of the helper class that constructs the slider:
Vector2 pos = node.WindowRect.position + rect.position * GridSpacing;
value = GUI.VerticalSlider(new Rect(pos, rect.size * GridSpacing), value, maxValue, minValue);
I can see why this doesn't do what I want, but I don't know how to go about it given the GUI controls aren't part of the GUI.Box.
Any help or suggestions, even a nudge towards another source would be greatly appreciated - I feel I've used all the search terms that exist in my head!
Edit - Solved: Thanks to Kleber for solving this one for me. In case anyone else runs into this or a similar issue, the solution for me was in realising that GUI controls consume left mousedown events automatically, so clicking a slider means there's no propagation to the Box to check if it was clicked.
What I needed to do was separate the IsSelected and IsDragged flags in the Node class, and clear IsDragged on mouseUp. I originally used IsSelected to flag both drag enabled, and selected (multiple nodes could be selected and dragged at once).
It's quite a complex tutorial so I didn't read it entirely, but the problem seems to be the MouseDrag detection. Well, basically you want to stop the event propagation when you click on a GUI element inside the Box, right? To do so, you call:
Event.current.Use()
every time the user drags the mouse on one of your components.
Using the resource you've mentioned, I altered the Node class and added a slider inside the Draw() method, ending like this:
public void Draw() {
inPoint.Draw();
outPoint.Draw();
GUI.Box(rect, title, style);
GUI.BeginGroup(rect);
_value = GUI.HorizontalSlider(new Rect(20, 0, 50, 20), _value, 100, -100);
GUI.EndGroup();
}
Another thing you can do is change how you draw your window. Here it's a simple example that I've tested on the latest Unity version (5.6):
private void OnGUI() {
GUI.Box(_rect, string.Empty);
GUI.BeginGroup(_rect);
_value = GUI.VerticalSlider(new Rect(145, 100, 20, 100), _value, 100, -100);
GUI.EndGroup();
var e = Event.current;
if (e.type == EventType.MouseDrag && _rect.Contains(e.mousePosition)) {
_rect.x += e.delta.x;
_rect.y += e.delta.y;
Repaint();
}
}
As you can see, this example doesn't need an Event.current.Use() to work properly.
Hi I have an animation in my LibGDX game that flickers when flipped.
So it switches really fast (like every frame) from flipped and not flipped and i know that the flip variable is not changing. Here's the code:
#Override
public void render(SpriteBatch sb, float a) {
updateDrawVariables(a);
if (isWalking) {
walkStateTime += Gdx.graphics.getDeltaTime();
}
TextureRegion frame = walk.getKeyFrame(walkStateTime, true);
if (flip) {
frame.flip(true, false);
}
sb.draw(frame, drawX, drawY, drawWidth, drawHeight);
if (Main.DEBUG)Resources.font.draw(sb, "HP: " + health, drawX, drawY);
}
Any answers is appreciated!
frame.flip(true, false);
This will always flip the texture region from it's current state. So it will be flipped one frame, not flipped the next and then flipped again.
You want something like
frame.flip(!frame.isFlipX(), frame.isFlipY());
I am using libGdx to code a Pong-like game.
In the GameScreen I want to display the remaining lives of each player.
For that, in the create() of the GameScreen I have a table containing several ImageButtons:
for(int i = 0; i < lifePlayer1; i++){
lives[i] = new ImageButton(skin, "Life");
lifeTable.add(lives[i]);
lives[i].setVisible(true);
lives[i].setTouchable(Touchable.disabled);
}
Each ImageButton represent a life. When a player loses a life, the ImageButton corresponding to the lost life is setChecked, and the imageChecked represents the lost life
Before the game start I want to display a message saying "Game Starts !". The message crosses the screen from left to right, and the game starts only when the message has disappeared.
The problem I encounter is that the instantiation of the lifeTable takes time, and the GameScreen starts running before the instantiation is finished. I explain :
When I disable the lifeTable, everything runs smoothly, the "Game Starts !" message crosses the screen from left to right and the game starts.
When I enable the lifeTable, when I press on the "New Game" button in my MainMenueScreen, there is a delay (let's say 0.5 seconds), like a loading time, and when the GameScreen is finally displayed, the "Game Starts !" message has almost finished crossing the screen.
I have a 2 players mode, with 2 lifeTable to load, and it's even worse, when the GameScreen displays, in 2 players mode, the "Game Starts !" message has already finished crossing the screen and the game has already started.
I am using an AssetManager, and every texture used in the game are already loaded before reaching the MainMenuScreen. The AssetManager helped to reduce the GameScreen loading time, but it's still not satisfying.
Is there something to preload the lifeTable, the same way we preload the textures with the AssetManager ?
Or, is there a way to have the render() of the GameScreen starts only when the create() has finished the instantiation of ever objects ?
Thank you for your help.
is probably not the most efficient way to make but which occurred to me while reading your question, and it is simple
Variable Class
private boolean loadForTest = false;
.
#Override
public void show() {
.//Other Code
image = new Image(YourAssetManager.get("YourImage.png", Texture.class));
image.addAction(Actions.sequence(
Actions.moveTo(Gdx.graphics.getWidth(),
(Gdx.graphics.getHeight()/2),
1f)));
image.setPosition((-100f), Gdx.graphics.getHeight()/2);
YourStage.addActor(image);
}
#Override
public void render(float delta) {
.//Other Code
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
if (loadForTest == false){
if (image.getActions().size == 0){
Gdx.app.log("test" ,"now load table");
for (int i = 0; i <lifePlayer1; i ++) {
vidas [i] = new ImageButton (piel, "Life");
lifeTable.add(lives[i]);
lives[i].setVisible(true);
lives[i].setTouchable(Touchable.disabled);
}
loadForTest = true;
}else{
Gdx.app.log("test" ,"Your Other Code Play");
}
}
}
Edit
added another code because my English is not very good and did not know very well what you wanted to do
Variable Class
private boolean loadForTest = false;
.
#Override
public void render(float delta) {
.//Other Code
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
if (loadForTest == false){
for (int i = 0; i <lifePlayer1; i ++) {
vidas [i] = new ImageButton (piel, "Life");
lifeTable.add(lives[i]);
lives[i].setVisible(true);
lives[i].setTouchable(Touchable.disabled);
}
loadForTest = true;
}else{
Gdx.app.log("test" ,"Your Image load, Your Other Code Play");
}
}
I have a 3dtext named Play, which when clicked will play the animation; the other one is named Back, which reverses the animation. Problem is after I Played and Backed it, the animation wont play anymore when i clicked Play.
The animation named redsubmenu is in legacy and clamp forever wrap mode.
public class PlayButtonScript : MonoBehaviour {
//public static PlayButtonScript pbs;
public GameObject redsubmenu;
void Update(){
#if UNITY_EDITOR
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray,out hit)){
if(hit.collider.name == "Play"){
redsubmenu.animation.Play();
}
}
#endif
}
}
public class BackButtonScript : MonoBehaviour {
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray, out hit)){
if(hit.collider.name == "Back"){
transform.parent.animation["redsubmenu"].speed = -1;
transform.parent.animation.Play("redsubmenu");
}
}
#endif
}
}
It appears that you never reset the speed of the animation back to 1. When you click play the first time the speed is initially 1, so it works fine. However, when you back you set the speed to -1 and it is never set to any other value.
Try using:
if (hit.collider.name == "Play") {
transform.parent.animation["redsubmenu"].speed = 1;
redsubmenu.animation.Play();
}
in your play button script.
You might also be able to make use of Animation.Rewind.
http://docs.unity3d.com/ScriptReference/Animation.Rewind.html
Just to be more specific, i edited my playbuttonscript as shown below:
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray,out hit)){
if(hit.collider.name == "Play"){
if(redsubmenu.animation["redsubmenu"].speed == -1){
redsubmenu.animation["redsubmenu"].speed = 1;
} else {
redsubmenu.animation.Play();
}
}
}
in my back button, i delete the transform.parent.animation.Play, no need for that.