how to get keyboard input to move things ?

조회 수: 3 (최근 30일)
Owais
Owais 2012년 4월 22일
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 :)

답변 (2개)

Daniel Shub
Daniel Shub 2012년 4월 22일
Graphics objects are not automatically refreshed. You might need to use drawnow.
doc drawnow
  댓글 수: 2
Owais
Owais 2012년 4월 23일
I didn't get you, can you write the code please ??
Daniel Shub
Daniel Shub 2012년 4월 24일
I am pretty confident I could write the code, but that really isn't the point of Answers.

댓글을 달려면 로그인하십시오.


Jan
Jan 2012년 4월 23일
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.

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by