I've been searching for easy way to get time step between TouchedMoved method calls in cocos2d-x, but so far I find nothing.. Could you help me out here?
You can accomplish it directly with the C++ primitives, follow this link:
http://www.cplusplus.com/reference/ctime/time/
You'll find a sample script which demonstrates how to calculate difference between two times.
Another way is to sum the delta time of the update method into an instance var, like this:
void YourClass::update(float dt)
{
m_timer += dt;
}
Then in your onTouchBegin, onTouchMoved and onTouchEnded methods get the value of m_timer and count the difference. For example:
void YourClass::onTouchBegin(cocos2d::Touch *touch, cocos2d::Event *event) {
float m_beginTime = m_timer;
}
void YourClass::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
float m_endTime = m_timer;
float time_diff = m_endTime - m_beginTime;
}
Related
I wasn't exactly sure how to title this, however I will explain what I mean. I am trying to make my game object move using:
transform.translate(Vector2.up \* moveSpeed * time.deltaTime);
Now what I want to do next is what is causing some trouble. I would like the object to continue moving until it hits the Y value of 1, and then turn left. I almost have that working. See it WILL work if I use >= 1, because it skips over the value of 1 (not moving by choppy ints). Even if I try changing it to == 1.0f it still won't work as I guess the object is moving too quickly and skips over some values. We've tried re-positioning it in the >= block of code but that bugs out and doesn't work very well.
If you have any thoughts or possible solutions that would be great! Thanks! :-D
Code ex:
if (transform.position.x <= -2 && transform.position.y < -1) {
transform.Translate(Vector2.up * playerSpeed * Time.deltaTime);
}
else if (transform.position.y <= -4)
{
transform.Translate(Vector2.left * playerSpeed * Time.deltaTime);
}
I can't really match what you want from your gif. Although, what you need is fairly simple when you think of adding a bool value called didCollidHappen and implement the OnCollideEnter method in your script.
Doing so, when you call Update at each frame, you will verify if there were any collisions and then call the code to make your game object jump. What follows is a code snippet. Insert it in your C# script :-)
public bool didCollideHappen =false;
public void OnCollideEnter(Collision col)
{
if(col.gameObject.tag == "Something")//if it's any game object, then just put bool value to true !!
{
didCollidedHappen = true;
}
}
public void MakeSlowedJump()
{
actualSpeed -= slowAmount * Time.fixedDeltaTime;
transform.position += new Vector3 (0.13F, actualSpeed * Time.fixedDeltaTime, 0);
}
public void Update()
{
if(didCollideHappen)
MakeSlowedJump();
}
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.
I'm trying to port some OpenGLES code from iOS to Android.
In iOS I have this code:
- (void)setColorOn:(BOOL)yes
{
glUniform1i(colorOnUniform, yes);
}
where the glUniform1i() method takes an Integer as a Uniform location and a Boolean.
In Android, the closest I can get is this:
public void setColorOn() {
GLES20.glUniform1i(colorOnUniform, 0);
}
where the glUniform1i() method takes an Integer as a Uniform location and another Integer, I think as a texture id...
I've dug around the Kronos documentation, but can't seem to find a proper translation ...
Thoughts ?
Wait what? The glUniform1i takes 2 integers in both cases. Why the developer inserted a boolean value I have no idea. Anyway the iOS boolean value translates as YES(true) = 1 and NO(false) = 0. This value is probably used in shader as if(colorOnUniform == 0)...; else...;
This method should probably be like:
- (void)setColorOn:(BOOL)yes
{
glUniform1i(colorOnUniform, yes?1:0);
}
Issue resolved...
I am making a Tron game in Processing. I have the game all worked out but I do not know how to add a reset option to start a new game after the player loses.
Anyone have some suggestions?
Well usually you should make a method that will reset/recreate/delete what is needed to restart your game. Like(pseudo):
void reset(){
score = 0;
ballsList.removeAll();
playerPositionX = 0;
playerPositionY = 0;
}
And then call it when needed.
Avoid using "init" as name of the method or you will override a built in method.
Wouldn't a simple switch case work fine ?
Switch (levels):
Case one:
Case last level:
If (this == that){
levels = one;
break
}
What I would say is wrap your whole gamecode in a function like void inGame(){gamecodeing} and when something happens like if (player.state == "dead"){inGame();} and the ingame at the starting as well. Like so:
void setup() {
size(500,500);
}
void draw() {
inGame();
if (playerHasLost) {inGame();}
}
void inGame() {gameStuff}
and every time inGame() is called it kind of does it all over again.
I'd reccomend running the setup() again.
And then store your variables in there like x = 0;, score = 0;.
I know there have been questions asked on this topic but they all differ slightly
what I want is a simple method that you pass an x value a y value and a quadrant value and it returns angle so far I have...
-(float)getAngle: (float)x yvalue:(float)y Quadrant:(float) quadrant{
float angle=0.0;
if(quadrant==1){
}
NSLog(#"%f",x);
return angle;
}
im not concerned about calculations at this point. I just want to know how to properly declare this method, because it wont let me name the first parameter.
Your code, as is, would work fine. Suppose you have a method like what you posted:
-(float)getAngle: (float)x yvalue:(float)y Quadrant:(float) quadrant{
float angle=0.0;
if(quadrant==1){
}
NSLog(#"%f",x);
return angle;
}
You could call this within the current class, and receive the returned value, like so:
// sample values added
float result = [self getAngle:-1.0 yvalue:0.5 Quadrant:2.0];
NSLog(#"%f",result);