how to get keyboard input to move things ?

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 :)

Réponses (2)

Daniel Shub
Daniel Shub le 22 Avr 2012
Graphics objects are not automatically refreshed. You might need to use drawnow.
doc drawnow

2 commentaires

Owais
Owais le 23 Avr 2012
I didn't get you, can you write the code please ??
Daniel Shub
Daniel Shub le 24 Avr 2012
I am pretty confident I could write the code, but that really isn't the point of Answers.

Connectez-vous pour commenter.

Jan
Jan le 23 Avr 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.

Catégories

Question posée :

le 22 Avr 2012

Community Treasure Hunt

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

Start Hunting!

Translated by