- Home /
Two methods are called on repet in my code and I don't know why
I'm following a video guide series from zenva academy and I'm making a frogger clone. When the frog goes over the top of the screen his position is reset to the start and the level value is supposed to increase by one along with the score value. Instead their methods keep getting called. I know the problem isn't the OnPlayerMoved and OnPlayerEscaped because they get called even if I only have one Debug.Log line in my code.
To make things simpler I'm editing this to just have my whole code, the flaws should come from the boundry touch method which resets the frog when he goes over the top of the screen:
public delegate void PlayerHandler();
public event PlayerHandler OnPlayerMoved;
public event PlayerHandler OnPlayerEscaped;
private bool jumped;
public float jumpDistance = 0.32f;
Vector2 targetPosition = Vector2.zero;
private bool tryingToMove = false;
private Vector2 startingPos;
// Use this for initialization
void Start () {
startingPos = transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
moveLogic();
boundryTouch();
}
void moveLogic()
{
float horizontalMovement = Input.GetAxis("Horizontal");
float verticalMovement = Input.GetAxis("Vertical");
if (!jumped)
//the frog didn't move
{
if (horizontalMovement != 0)
{
tryingToMove = true;
targetPosition = new Vector2(
transform.position.x + (horizontalMovement > 0 ? jumpDistance : -jumpDistance),
transform.position.y);
}
else if (verticalMovement != 0)
{
tryingToMove = true;
targetPosition = new Vector2(
transform.position.x,
transform.position.y + (verticalMovement > 0 ? jumpDistance : -jumpDistance));
}
Collider2D hitCollider = Physics2D.OverlapCircle(targetPosition, 0.1f);
if (tryingToMove == true && hitCollider == null)
{
transform.position = targetPosition;
jumped = true;
if (OnPlayerMoved != null)
{
OnPlayerMoved();
}
}
}
else
//the frog moved
{
if (horizontalMovement == 0 && verticalMovement == 0)
{
jumped = false;
}
}
}
void boundryTouch()
{
if (transform.position.y < -(Screen.height / 100f) / 2f)
//check if frog goes through the bottom
{
transform.position = new Vector2(transform.position.x, transform.position.y + jumpDistance);
}
if (transform.position.y > (Screen.height / 100f) / 2f)
//check if frog goes through the top
{
transform.position = startingPos;
if (OnPlayerEscaped != null)
{
OnPlayerEscaped();
}
}
if (transform.position.x < -((Screen.width / 100f) / 2f) + 0.32)
//check if frog goes through the left
{
transform.position = new Vector2(transform.position.x + jumpDistance, transform.position.y);
}
if (transform.position.x > ((Screen.width / 100f) / 2f) - 0.32)
//check if frog goes through the right
{
transform.position = new Vector2(transform.position.x - jumpDistance, transform.position.y);
}
}
Here's the GameManager script:
public Player player;
public Text levelText;
public Text scoreText;
private float highestPos;
private int score = 0;
private int level = 1;
// Use this for initialization
void Start () {
player.OnPlayerMoved += OnPlayerMoved;
player.OnPlayerEscaped += OnPlayerEscaped;
highestPos = player.transform.position.y;
}
// Update is called once per frame
void Update () {
}
void OnPlayerMoved()
{
if (player.transform.position.y > highestPos)
{
highestPos = player.transform.position.y;
score++;
scoreText.text = "Score: " + score;
}
}
void OnPlayerEscaped()
{
highestPos = player.transform.position.y;
level++;
levelText.text = "Level: " + level;
}
}
What do you mean by "their methods keep getting called"?
Which method(s)?
"...keep getting called" suggests the method's being called on every frame (from the time they escape). Is that what you meant? If not, then what?
"Ins$$anonymous$$d..." suggests that the position is not being reset and the level is not being increased. Is that what you meant? (you say that OnPlayerEscaped is being called which seems contradictory)
Answer by stuey0016 · Jan 06, 2019 at 05:45 PM
@unity_oLFfsDJmlThqUw it's hard to know exactly why your methods keep getting called without seeing the entirety of your code, but I am curious as to when you set the member variable tryingToMove = false
. If you're not doing that at the start of every loop, then that might explain why you're seeing your handler methods called repeatedly.
If that's not the problem, perhaps you could post more of your code regarding that loop? Is that block of code called inside Update() or FixedUpdate()?
I called tryingTo$$anonymous$$ove = false at the top of the Player script which might be in the wrong spot.
Yeah I'll just post all my code, this is the Player script:
@stuey0016 just had to edit the normal post to show all my code, I made the moveLogic and boundryTouch methods just for formatting but maybe that's part of the problem? I'm still kinda new to coding I appreciate the help.
Your answer
Follow this Question
Related Questions
Getting the Player in a Script! 2 Answers
Multiple Cars not working 1 Answer
Move Multiple Directions At Once (ex. forward and right) unity 3d 1 Answer
Distribute terrain in zones 3 Answers