how to get keyboard input to move things ?

4 visualizaciones (últimos 30 días)
Owais
Owais el 22 de Abr. de 2012
Hi ppl,
I want to get keyboard input so that i can move my player to that location. the code i wrote is not moving the ball. here is the code, can any body tell what is the error in my code.
function [ ball ] = move( ball )
%MOVE Summary of this function goes here
% Detailed explanation goes here
set(ball.fig, 'KeyPressFcn', 'a = get(gcf, ''CurrentCharacter'')');
a = 1;
if double(a) == 28 %left arrow
ball.manMid(1) = ball.manMid(1) - 5;
elseif double(a) == 29 %right arrow
ball.manMid(1) = ball.manMid(1) + 5;
end
set(ball.man,'Position',[ball.manMid, 2*ball.manRad 2*ball.manRad]);
end
and also i want my player to fire when spacebar is pressed, how can i do that.
Thank You :)

Respuestas (2)

Daniel Shub
Daniel Shub el 22 de Abr. de 2012
Graphics objects are not automatically refreshed. You might need to use drawnow.
doc drawnow
  2 comentarios
Owais
Owais el 23 de Abr. de 2012
I didn't get you, can you write the code please ??
Daniel Shub
Daniel Shub el 24 de Abr. de 2012
I am pretty confident I could write the code, but that really isn't the point of Answers.

Iniciar sesión para comentar.


Jan
Jan el 23 de Abr. de 2012
The code is very unclear. Where is this function called from? It is not a callback, which is triggered by pressing a key. Setting the KeyPressFcn inside the function is unexpected. Then it is strange, that the callback function assigns a in the base-workspace. This is not directly accessible from inside the function.
a = 1;
if double(a) == 28
This is confusing also. Do you expect, that the callback function for KeyPress modifies a and restarts the function in the if double(a) == 28 line?
Currently it is hard to improve your function, because it can only be guessed, what you want to achieve. I suggest to use your favorite search engine to look for other KeyPressFcn examples in the net.

Categorías

Más información sobre Desktop en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by