label over label overwriting , while displaying the health lable in cocos2d android - android

I have to display the palyers lifes in my game. every time the player hits the wrong enemy the lifes decrease.the default lifes are 5.
My code is as follows
first I declare this in my gamelayer
static int lifes=5;
CCLabel _lifes;
then a method as follows at bottom of the code
public void showLable(CCLabel _lifes){
if(_lifes != null){
this.removeChild(_lifes,true);
}
_lifes = CCLabel.makeLabel("" + lifes, "Verdana", 20);
_lifes.setColor(ccColor3B.ccbrown);
_lifes.setPosition(winSize.width/2,(winSize.height/2));
addChild(_lifes,3)
}
then I wrote this condition where the player hits the wrong enemy
lifes--;
showLable(_lifes);
1) everything works fine, the lable is displayed and label gets decreased but the label does not show up until the player hits the wrong enemy, as you see I gave default value as 5, the label shows up after the player hits wrong enemy ie, from 4.
2) Another major problem is the label displayed is displaying without removing the previous value. For ex. the lifes are 5 as default. 5 is displayed when when game starts. when the life decreases the lifes value should be 4, So here in my game the 4 is placed on 5 itself.
And then the lifes are placed on the same number as 3 or 2 or 1. All the nuumbers show on each other. Now I think you understand me
thanks in advance

1) check that your are calling showLable(_lifes); at the start of your game after its initialized, so the label show up with the value 5.
2) I'm not really sure about it but CCLabel should extends a CCSprite and thus have a setVisible method.
you may try something like :
if(_lifes != null){
_lifes.setVisible(false);
this.removeChild(_lifes,true);
}
I only found the c++ api reference version :
http://www.cocos2d-x.org/reference/native-cpp/d4/de7/classcocos2d_1_1_c_c_sprite.html

Related

How to make a game object appear through triggers from 3 different game objects

I am developing an AR app using Unity3D and Vuforia.
I did all the basics of AR and now in the process of completing it.
However, I am currently stuck at the triggers part.
I have 3 flashcards (Number 1, Number 2, Plus). I have put box colliders in each one of them and rigidbodies in Number 1 and Number 2. The Plus card acts as the trigger.
Right now, I am trying to make the game objects in each card disappear when triggered, and a fourth game object will appear when the trigger happens. The fourth game object is the result of the addition (i.e. 1 + 2 = 3).
I have developed the following code. The game objects are triggered according to the tags I have given them. The game objects disappear, but the fourth game object doesn't appear. What have I done wrong here?
#pragma strict
var mathplus : GameObject;
var sphere01 : GameObject;
var sphere02 : GameObject;
var result03 : GameObject;
function Start(){
sphere01.SetActive(true);
sphere02.SetActive(true);
mathplus.SetActive(true);
result03.SetActive(false);
}
function OnTriggerEnter (other : Collider) {
Debug.Log("Object entered the trigger.");
if (other.tag == "number01" && other.tag == "number02"){
mathplus.SetActive(false);
sphere01.SetActive(false);
sphere02.SetActive(false);
result03.SetActive(true);
}
}
function OnTriggerExit (other : Collider) {
Debug.Log("Object exited the trigger.");
mathplus.SetActive(true);
sphere01.SetActive(true);
sphere02.SetActive(true);
result03.SetActive(false);
}
The game objects in the Number cards are called "sphere", the game objects in the Plus card is called "mathplus", and the fourth game object is called "result03".
I hope someone can help rectify this problem.
Thanks and regards.
You made a small mistake.
It is because the fourth card is disabled, so the collider of it is disabled too.
Your logic is right, what you should do is invisible the card4, not disable it
Disable Mesh Rendering To Hide A Gameobject
GetComponent(MeshRenderer).enabled = false;
It is c# script, sorry, I do not know very about unity js.😆
You can get more detail from invisible Gameobject and disable Gameobject

Android Studio Libgdx Animation

I am trying to do an animation whenever my character jumps. It works in the following way: when I jump, I first draw the animation of my char. If it is finished, I start modifying his position and also draw another texture, for the inAir moment. The problem is that the animation runs instantly (I tried putting the speed 1 frame per second,but it still runs instantly) and the character jumps before the animation is done. I suspect it considers it finished before it actually finishes. This is how I check if the animation is finished: first I check if the animation start (the jump button is pressed) and then I have an if condition like this:
if (animationStarted && rightJumpAnim.isAnimationFinished(System.currentTimeMillis()))
{
animationStarted = false;
animationFinished = true;
timePassed1 = 0;
}
After this, I put animationFinished to false when he touches the ground again. Any advice?
P.S. here is the code for running the jumpAnimation:
if (toDrawJumpRight1 == true) {
animationStarted = true;
timePassed1 += Gdx.graphics.getDeltaTime();
spriteBatch.draw(rightJumpAnim.getKeyFrame(timePassed1, false), posX + cameraX, posY + cameraY, sizeX, sizeY);
}
Your problem should be in the very first line you provided to us:
if (animationStarted && rightJumpAnim.isAnimationFinished(System.currentTimeMillis()))
The argument you must pass is the STATETIME of the animation.
if (animationStarted && rightJumpAnim.isAnimationFinished(timePassed1))
Edit:
Some more Informations to this problem:
You say you want 1 frame stay 1 Second.
Now we say, you have approx 5 frames.
5frames * 1 second = 5seconds for finish
so if statetime is over 5 seconds, your animation is played ONE time (is finished)
Animation.isAnimationFinished(float stateTime) checks now, if the current stateTime from your animation is higher than the 5 seconds.
System.currentTimeMillis is at this moment:
1440618906341
now.. whats bigger?
Have a nice Evening (I recommend using TimeUtils.millis instead of System.currentTimeMillis, guess it's optimated, fo' sure.)

load level dynamically using andengine in android

I am developing a game using andEngine in android, I've developed animated sprites background, platforms and scores. for loading level, I've made a
.lvl
extension file in which I've defined platforms, player and obstacles at fixed positions. I want to know is there anything provided by andEngine so that I can dynamically load obstacles, platforms, coins and other objects rather than loading any file, my aim is to keep my game-world going as long as my player is alive.
I think I am clear.
Thanks!!!
I would like to point you to investigate "TimerHandler" as your dynamic time handler (Defining how often to attach another object/entity.
With a time handler in place, you can assign nearly any int/double/float value of an object/entity with MathUtils.random(min, max).
Example:
TimerHandler objectTimerHandler = new TimerHandler(1f, true, new ITimerCallback() {
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
int timerSeconds = 0;
timerSeconds++;
final Sprite object1 = new Sprite(0, 0, resourcesManager.object1_region, vbom);
if (timerSeconds == MathUtils.random(1, 4))
{
attachChild(object1);
object1.setX(MathUtils.random(0, 480));
}
});
registerUpdateHandler(objectTimerHandler);
So, the TimeHandler "objectHandler" is called every 1 seconds defined by the 1f variable.
An integer counter "timerSeconds" is setup as a base.
The Sprite object/entity is initialized.
Now the "magic": the if boolean is in place to set a sense of true randomness. So here's the math... Every second passed, add a "timerSecond". Then, IF "timerSecond" is EQUAL to a random number between 1-4, then attach the sprite "object1" and set it's X position, also on a random range 0-480.
In reality, this means for every 1 second passed, there is a 1/4 chance that this sprite will be created/attached. (this can be altered to any desired chance range by changing that random range max value. EG: MathUtils.random(1, 100) <-- this would state a 1 in 100 chance to spawn the sprite each second.)
The 2nd to last line closes the Timehandler code, and the last line registers the Timehandler to the class/scene/entity/engine (ect..).

refreshing of canvas is happening only after touch

I am using ViewPagerparallax class,(for parallax view ) and I am setting the background using pager.setbackgroundAsset(....);
everything works fine....but I have a situation,what i want to achieve is that whenever the fragments go in a pause state and whenever it is resumed,the background should change and hence i am changing the background in the onResume method but seems like change is only visible whenever i try to swipe between fragments ...
this is my code
public void onResume()
{
Activity_Fragments ab = (Activity_Fragments) this.getActivity();
ViewPagerParallax pager=ab.hi();
int arr[]={R.raw.a1,R.raw.a2,R.raw.a3,R.raw.a4,R.raw.a5,R.raw.a6,R.raw.a7,R.raw.a8,
R.raw.a9,R.raw.a10,R.raw.a11,R.raw.a12,R.raw.a13,R.raw.a14,R.raw.a15,R.raw.a16,
R.raw.a17,R.raw.a18,R.raw.a19,R.raw.a20,R.raw.a21,R.raw.a22,R.raw.a23,
R.raw.a24,R.raw.a25,R.raw.a26,R.raw.a27,R.raw.a28,R.raw.a29};
int x=(int)(Math.random() * ((28 ) + 1));
pager.setBackgroundAsset(arr[x]);
A few notes.
First of, I am assuming you are using this: https://github.com/MatthieuLJ/ViewPagerParallax/blob/master/src/com/matthieu/ViewPagerParallax.java
When looking at the source it appears to be that you are doing things correctly.
I few things I can see that could be happening
The max number of pages set is 0, which is the default
The width or height is 0
The background hasn't changed from last time
The application has run out of memory (They say they don't want to use more then 1/5th of the memory
An exception was thrown while creating the bitmap of the image
A few things you could try
For point number 1, if you haven't already, call the set_max_pages with a number you see fit.
Point 2, Is the ViewPager in the activity with width and height set?
Point 3, confirm that you generator is working. (Already done, logic works perfect)
int arr[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20,21,22,23,24,25,26,27,28,29};
for(int i = 0; i < 100; i++){
Log.d("TEST", String.valueOf(arr[(int)(Math.random() * ((28 ) + 1))]));
}
This returns the desired output, no problem there.
Point 4 & 5, Check logcat, it could say some errors, the tag should say ViewPagerParallax
I would say point 1 could be it, it will draw the background if it hasn't already once you set it.
I'm not really sure how setBackgroundAsset works, but try a call to invalidate(); at the end of onResume(). That will insure that your onDraw() method will be called ASAP and the new background will be redrawn.

animation made in adobe flash professional

I have made 2 different animations in Adobe flash professional cs5.5 for an Android aplication.
And I want a code that makes it possible for a user of the app to play the animation as often they want, so if the user wants the animation to play 1 time the first animation will be playes, if the user wants to play it 2 times the animation 1 and 2 will be played, if the user wants the animation 3 times played the animation 1, 2 and 1 will be played and so on.
Can somebody help me with this problem and tell me if this is possible in jquery.
If I was you, the way I would approach this is by having a keyframe after each of the animations where you can type some code.
On the menu page or where ever you have the code for how many times the code should run, define a variable and call it something like "runTimes" which should become the amount of times the animation should run.
At the end of the animations do a simple if statement to check what the value of "runTimes" is and then decrement it. Depending on the value, it should use gotoAndPlay/gotoAndStop.
So, put this on the keyframe after the first animation:
if (runTimes > 0) {
runTimes--;
gotoAndPlay(<FIRST FRAME OF SECOND ANIMATION>);
} else {
gotoAndStop(<FRAME OF MAIN MENU>);
}
and this after the second animation:
if (runTimes > 0) {
runTimes--;
gotoAndPlay(<FIRST FRAME OF FIRST ANIMATION>);
} else {
gotoAndStop(<FRAME OF MAIN MENU>);
}
On the mainmenu frame, let's assume you have a textbox named "numTimes_txt" for the number of times to play and a button "playAnimations_btn" to start the animations.
import flash.events.MouseEvent;
var runTimes:int = 0;
playAnimations_btn.addEventListener(MouseEvent.CLICK, playAnims);
function playAnims(e:MouseEvent):void {
runTimes = parseInt(numTimes_txt.text);
play(); // or gotoAndPlay(<FIRST FRAME OF FIRST ANIMATION>);
}
I haven't tested this as I don't have my IDE on me right now, but this should work if I understand the question properly.

Categories

Resources