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();
}
Related
I am quite new to programming and decided to make a little game, so far I have only made a small block that is able to move in all 4 directions (left, right, up and down). Pretty simple and nothing extra-ordinary. However, when I run my program, sometimes it will stop working, not as in crashing causing Processing itself to crash, but my program will just end.
As far as my testing goes, I think this happens when I press two keys at the same time (like W and S). Does anyone happen to know what causes it to stop, and perhaps how to fix it as well?
void setup(){
size(1080,720);
frameRate(30);
}
int shipLR = 0; //Variable for the ship to go left/right
int shipUD = 0; //Variable for the ship to go up / down
void draw(){
background(0);
shipLR = constrain(shipLR, 0, 1040); //Constrain the ship in the window
shipUD = constrain(shipUD,0,680); // Constrain the ship in the window
move();
Shuttle();
}
void Shuttle(){
rect(shipLR, shipUD, 40,40); //Draw the ship
}
void move(){
if (keyPressed) {
if (key == 'a') {
shipLR = shipLR - 20; // Go left
return;}
if (key == 'd') {
shipLR = shipLR + 20; // Go right
return;}
if (key == 'w'){
shipUD = shipUD - 20; // Go up
return;}
if (key == 's') {
shipUD = shipUD + 20; // Go down
return;}
}
}
Any help will be greatly appreciated.
Edit: I found something that causes this problem to occur more frequent; If I implement a frameRate of a value lower than 60 (currently trying with 30) this happens more often.
Edit 2: With the suggestions below of making my move function in a single if block had a lot of good impact. The program no longer stops when I move the ship in the middle of the window it no longer stops, it now only does it at rare occasions when I bump too often against the borders of the window. Perhaps it has something to do with constrain?
in move() (java naming conventions specify that method names start with lower case) you should exit on the first key pressed found. the code can be re-organized to reduce multiple identical conditions:
void move(){
if (keyPressed) {
if (key == 'a') {
shipLR = shipLR - 20; // Go left
return;
}
if (key == 'd') { // still inside if keyPressed
shipLR = shipLR + 20; // Go right
return;
}
if (key == 'w') {
...
}
} // end of if keyPressed
}
My guess it that the program doesn't "stop working" or end- it's just that your rectangle stops moving. If you let go of all the keys, then push just one key, I'd bet that the rectangle starts moving again. (In the future, please try to be more specific- saying "it stops working" isn't very helpful.)
Anyway, that's because of the way you're handling the key presses. What happens if you press a key other than the ones you're checking for?
You'll enter the move() function, and you'll enter the if(keyPressed) block, but you won't enter any if the other if statements inside that block. The key variable only holds the most recently pressed key.
Depending on what you want to do, you should either refactor those if statements to only change the direction if the key was valid, and to ignore all other key presses. Or you might want to use a set of boolean variables to track multiple keys being pressed at the same time. More info on that approach can be found here.
If you still can't get it working, then please try to be more specific about exactly what happens.
I'm an Italian student and I'm new in programming. I need your help for a school project.
I'm making a blob tracking program using Daniel Shiffman's tutorials. Currently I have 2 blobs on the screen. I am identifying them with 2 IDs: number 0 and number 1.
I need to put some conditions on those blobs: if one blob is in a certain part of the screen and the other one is in another part, I need to call a function.
I don't know how to put the if conditions separately for the two ids. Below is some pseudo code of what I would like to achieve:
for (id==0)
if (...) and
for (id==1)
if(...) then {
void()
}
I would really appreciate any help!
I don't really know where you want the blobs to be when the desired function fires, but I can try to give you an example...
Blob
Assign some sort of position variable, in this case PVector, to your blob object.
class Blob {
PVector position;
Blob (PVector position) {
this.position = position;
}
void update() {
*random movements, etc...*
}
}
Create two blob objects
Create two objects and assign a position to each of them.
Blob[] blobs = new Blob[2];
void setup() {
size(400, 400);
blobs[0] = new Blob(5, new PVector(40, 40));
blobs[1] = new Blob(13, new PVector(100, 100));
}
Check if blobs is at left or right side of the screen
I check if blob[0] is at the left side of the screen and if blob[1] is at right side of the screen. If they are, at the same time, the desiredFunction(); will fire.
void draw() {
for (int i = 0; i < blobs.length; i++) {
blobs.update();
}
if (blobs[0].position.x < (width / 2) && blobs[1].position.x < (width / 2) {
desiredFunction();
}
}
Remember
This is just an example. You could of course check other parts of the screen instead of the left and right parts. You can also use IDs on your blobs instead of an array, I just thought it was better to just use an array in this case.
PS: I wrote this answer without having processing started. The code has certainly a couple of typing errors.
For the example you have described, you can achieve this using the && operator in one if statement.
First assign the conditions you want to test to boolean variables. For example, create the boolean variables id0IsThere, and id1IsThere, and set them to true if the blobs are in the locations you want them to be in. Then use the following if statement:
if (id0IsThere && id1IsThere) {
yourFunction();
}
The && operator means that the code inside the if statement that executes yourFunction() is only executed if both conditions are true. In this case, if both blobs are in the positions you want them to be in. Hope that helps. Read more about if statements and the && operator here:
https://processing.org/reference/if.html
https://processing.org/reference/logicalAND.html
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;
}
I am working on movie in processing. The move.jump() function not working during movie reverse play. And I have reverse code:
(The reverse is global boolean variable)
float playSpeed = 1.0;
//After click reverse button
if(reverse == false){
playSpeed = playSpeed * -1;
movie.speed(playSpeed);
reverse = true;
}
else{
playSpeed = playSpeed * -1;
movie.speed(playSpeed);
reverse = false;
}
During reverse == true I click some button, and jump to a time, for example 5.0, code is show below:
//after click button
if(reverse == true){
movie.jump(5.0);
}
And this jump function is not work. It not jump to the movie frame which at 5.0 second. And show some error:
ast_segment_set_seek: assertion 'start <= stop' failed
Can anybody tell me why the jump function not work and how to fix it?
Thank you.
EDIT: more reasonable answer: Could it be that jump calculates the position in relation to the frame rate which becomes negative when playing backwards and then end is before start? In this case my workaround should work.
Not sure what classes you are using, but couldn't you just make a workaround? Should be so fast the user won't notice:
if (reverse) {
movie.speed(1f);
// jump();
movie.speed(playSpeed);
} else {
// jump();
}
Btw reverse can be written as (no if needed):
reverse = !reverse;
playSpeed = -playSpeed;
movie.speed(playSpeed);
I know the answer, I figure out by myself.
If movie play speed is less 0, the star will less than stop.
So, if the play speed is less 0, just * -1, let greater than 0, than jump, than * -1, let speed less than 0.
if(reverse == true){
m.speed(playSpeed*-1);
m.jump(5.0);
m.speed(playSpeed*-1);
}
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;.