Java ME. How to animate Sprite - animation

I am having trouble with animating my sprite in Java ME.
if ((k & FIRE_PRESSED) != 0) {
spriteActive = true;
boxer.nextFrame();
if (boxer.getFrame() == boxer.getFrameSequenceLength() - 6) {
spriteActive = false;
}
}
}
// TO re-start a game...
public void update() {
if(boxer.getRawFrameCount() == 5 && spriteActive == false){
boxer.setFrame(0);
}
}
When the enter key is pressed, spriteActive is set true but only changes the frame by one. I intend to have it animating the entire sequence but it is not doing that. Just animates 1 frame at a time with every press.
Does anyone have any ideas/advice of how I should approach this?
Thanks for the time and help!

Do you have any code that checks whether spriteActive is true and then sets the next frame?
I am no expert on Java but I would suspect you'd need to implement something like this:
if (spriteActive == true)
{
boxer.nextFrame();
}

Related

How to solve, Keyboard hide automatically when we work with multiple view?

We have two search textbox that will dynamically showing. One for portrait view and second for landscape view. We handled input text using flag in ViewDidLayoutSubviews() method. We have written a keyboard hide notification method as KeyboardWillHide(NSNotification notification) for text value handling.
We are getting these issue. Please have a look
1. When we start typing ViewDidLayoutSubviews() method every time call on key press and reload textbox. So my keyboard unable to up. We have fixe this with checking a flag if keyboard will then return.
2. Now we are getting same issue that keyboard hide automatic when we press on mic for speak to write. KeyboardWillHide(NSNotification notification) method every time call and hide keyboard. Its also call when we touch on tableview. 
How to solve these issue ,Please help
private void KeyboardWillHide(NSNotification notification)
{
string orientation = this.InterfaceOrientation.ToString();
if ((orientation.ToString() == "Portrait" || orientation.ToString() == "PortraitUpsideDown") && KeyboardHideSameView == false )
{
SearchText= txtSearchKeywordLS.Text;
txtSearchKeyword.Text = txtSearchKeywordLS.Text;
}
else if ((orientation.ToString() == "LandscapeRight" || orientation.ToString() == "LandscapeLeft") && KeyboardHideSameView == false )
{
SearchText = txtSearchKeyword.Text;
txtSearchKeywordLS.Text = txtSearchKeyword.Text;
}
txtSearchKeyword.ResignFirstResponder();
txtSearchKeywordLS.ResignFirstResponder();
isKeyboardApear = false;
KeyboardHideSameView = false;
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
if (isKeyboardApear == true) return;
applyViewGradient();
var orientation = this.InterfaceOrientation;
if (orientation.ToString() == "Portrait" || orientation.ToString() == "PortraitUpsideDown")
{
PortraitConst(this.View.Bounds.Height, this.View.Bounds.Width);
this.vwLandscapeHead.Hidden = true;
this.vwPortraitHead.Hidden = false;
this.txtSearchKeyword.Text = SearchText;
txtSearchKeyword.ResignFirstResponder();
txtSearchKeywordLS.ResignFirstResponder();
}
else if (orientation.ToString() == "LandscapeRight" || orientation.ToString() == "LandscapeLeft")
{
//Console.WriteLine("Landscape Mode");
// this.addSubview(landscapeView, containerView);
LandscapeConst(UIScreen.MainScreen.Bounds.Height, UIScreen.MainScreen.Bounds.Width);
applyViewGradient();
this.vwPortraitHead.Hidden = true;
this.vwLandscapeHead.Hidden = false;
this.txtSearchKeywordLS.Text = SearchText;
txtSearchKeyword.ResignFirstResponder();
txtSearchKeywordLS.ResignFirstResponder();
}
// this.containerView.NeedsUpdateConstraints();
}
We have solved it logically by using flags.
We have declare a boolean flag isKeyboardAppear=false when view appear first. When keyboard appear we set this flag to true And we noticed ViewDidLayoutSubviews() method are calling on every key press, so we write this code
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
if (isKeyboardAppear == true)
{
return;
}
}
When we hide keyboard manually we reset flag isKeyboardAppear=false .

Libgdx animation controller, optimize for culled animations

I wan't to cull all animations that are not visible to the user. I have tried a crude way where I simply do not call AnimationController.update() if the animated object is not visible. This does not work as I wan't though. Since when a culled object comes back into view it will play the last applied animation (even ones that I applied when they where culled) This is as expected I guess. Animations still needs to be applied to culled object as they might come into view halfway through an animation for example.
I am now looking at subclassing AnimationController, and change the update() method to take into consideration if the object is culled or not. If it is I wan't to only update the "time" and not do any "heavy" operations at all.
My problem is that I am not sure what are the heavy parts an what is the bare minimum I need to update/advance. So I was hoping someone with more insight to the AnimationController class could help me out a bit? Any help welcome, thanks!
/** Update any animations currently being played.
* #param delta The time elapsed since last update, change this to alter the overall speed (can be negative). */
public void update (float delta) {
if (paused) return;
if (previous != null && ((transitionCurrentTime += delta) >= transitionTargetTime)) {
removeAnimation(previous.animation);
justChangedAnimation = true;
animationPool.free(previous);
previous = null;
}
if (justChangedAnimation) {
target.calculateTransforms();
justChangedAnimation = false;
}
if (current == null || current.loopCount == 0 || current.animation == null) return;
justChangedAnimation = false;
updating = true;
final float remain = current.update(delta);
if (remain != 0f && queued != null) {
inAction = false;
animate(queued, queuedTransitionTime);
queued = null;
updating = false;
update(remain);
return;
}
if (previous != null)
applyAnimations(previous.animation, previous.offset + previous.time, current.animation, current.offset + current.time,
transitionCurrentTime / transitionTargetTime);
else
applyAnimation(current.animation, current.offset + current.time);
updating = false;
}
I was hoping I could do something like only advance transitionCurrentTime, remain and current.time when culled...?

If Rectangle doesn't Contains Mouse Position

I have a Rectangle which I can touch with this command below.
if ((mouse.LeftButton == ButtonState.Pressed )&&
TextureRectangle.Contains((int)MousePos.X,(int)MousePos.Y))
{
// Action;
}
But is there a Command like "Not Contains", so I wanna do something else if the user touch out of the "TextureRectangle" area?
When I click to the Rectangle that both actions starts. I really dont know where the problem is.
if (mouse.LeftButton == ButtonState.Pressed){
if(TextureRectangle.Contains((int)MousePos.X, (int)MousePos.Y)) {
music1.Play();
}
else{
music2.Play();
}
}
my problem is that music1 and music2 plays at same time if i click on the Rectangle, i want that when i click on the Rectangle that music1 plays only (here is the problem , both starts to play)and when i click out of the Rectangle should start only music2 to play ( this case is ok)
I would strongly recommend you to get a programming book / ebook and start reading it. This is basic computer logic stuff.
if (mouse.LeftButton == ButtonState.Pressed)
{
if (TextureRectangle.Contains((int)MousePos.X, (int)MousePos.Y))
{
// inside
}
else
{
// outside
}
}
OR
if (mouse.LeftButton == ButtonState.Pressed)
{
if (!TextureRectangle.Contains((int)MousePos.X, (int)MousePos.Y))
{
// outside
}
else
{
// inside
}
}

I have two 3dtext, one plays the animation, the other reverses it. After one go, the animation wont play anymore, why?

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.

Optimized keyboard controls for XNA game

The code I am using for controlling the four-directional movement of the player's sprite in my 2D game is exhibiting some unwanted affection. I realize that this affection is because the if conditions that are first met will trump the later else ifs... So the directional affection my code shows now is: left > right > up > down.
What kind of affection I want is: the first direction pressed > the second direction pressed > the third direction pressed > the fourth direction pressed.
I also want it to remember what order the keypresses are in untill they're released.
Example:
I hold left, the sprite moves left.
I push up while still holding left, and the sprite immediately moves up.
I release up while still holding left, and the sprite resumes its movement left.
This memory should encompass all four directional keys so that the controls won't feel buggy if the user has "fat fingers".
This is the code I use for movement so far:
if (CurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
Speed.X = moveSpeed;
Direction.X = moveLeft;
}
else if (CurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
Speed.X = moveSpeed;
Direction.X = moveRight;
}
else if (CurrentKeyboardState.IsKeyDown(Keys.Up) == true)
{
Speed.Y = moveSpeed;
Direction.Y = moveUp;
}
else if (CurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
Speed.Y = moveSpeed;
Direction.Y = moveDown;
}
I am thinking I could use a List and just put the direction pressed (left, right, up, down) as strings into the list if it isn't already in the list, and then always check what the latest item in the list is to decide what directio to move. And of course remove the strings when the corresponding keys are released. Would this be a good way of solving it?
Here is my attempt on this:
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
if (!keyDownList.Contains("left"))
{
keyDownList.Add("left");
System.Diagnostics.Debug.WriteLine("left inserted");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Left))
{
keyDownList.Remove("left");
System.Diagnostics.Debug.WriteLine("left removed");
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
if (!keyDownList.Contains("right"))
{
keyDownList.Add("right");
System.Diagnostics.Debug.WriteLine("right added");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Right))
{
keyDownList.Remove("right");
System.Diagnostics.Debug.WriteLine("right removed");
}
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
if (!keyDownList.Contains("up"))
{
keyDownList.Add("up");
System.Diagnostics.Debug.WriteLine("up added");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Up))
{
keyDownList.Remove("up");
System.Diagnostics.Debug.WriteLine("up removed");
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
if (!keyDownList.Contains("down"))
{
keyDownList.Add("down");
System.Diagnostics.Debug.WriteLine("down added");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Down))
{
keyDownList.Remove("down");
System.Diagnostics.Debug.WriteLine("down removed");
}
try
{
if (keyDownList[keyDownList.Count-1].Contains("left"))
{
//move left
speed.X = moveSpeed;
direction.X = moveLeft;
}
else if (keyDownList[keyDownList.Count-1].Contains("right"))
{
//move right
speed.X = moveSpeed;
direction.X = moveRight;
}
else if (keyDownList[keyDownList.Count-1].Contains("up"))
{
//move up
speed.Y = moveSpeed;
direction.Y = moveUp;
}
else if (keyDownList[keyDownList.Count-1].Contains("down"))
{
//move down
speed.Y = moveSpeed;
direction.Y = moveDown;
}
}
catch (Exception e)
{
}
I had some problems with it initially, but it seems to work fine now with the exception of it generating exceptions (A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll) while my sprite is standing still. Any tips on how to stop that?
I'm not just looking for a solution that works, but for something durable and efficient that feels rock solid and professional, so discussion on the topic is more than welcome.
Durable, efficient, rock solid, etc. it may not be, but what you're saying with your if/else if block there is that you're only interested in one keystate per frame, when I don't think that's the case.
What happens if you try:
if (CurrentKeyboardState.IsKeyDown(Keys.Down) & !CurrentKeyboardState.IsKeyDown(Keys.Up))
{
Speed.Y = moveSpeed;
Direction.Y = moveDown;
}
if (CurrentKeyboardState.IsKeyDown(Keys.Up) & !CurrentKeyboardState.IsKeyDown(Keys.Down))
{
Speed.Y = moveSpeed;
Direction.Y = moveUp;
}
And repeat similar for left and right. By testing mutual exclusivity between opposing directions, you keep yourself from adding to and subtracting from direction in the same frame. Also, by using separate conditions instead of an if/elseif chain, you allow the possibility to process Left + Up in the same frame.

Resources